如何在r中拨打或加速函数内的数据帧变量
我正在努力在R中创建一个函数,该函数使用data.frame的变量名称作为其参数的一部分。
例如,我有这些数据
test.df <-
data.frame(
variable_1 = sample(letters[1:4],10, replace = T),
variable_2 = rnorm(10,10,3),
variable_3 = rnorm(10,40,15))
test.df
variable_1 variable_2 variable_3
1 c 5.514034 59.23525
2 a 10.515690 31.94552
3 d 11.845118 47.39481
4 c 8.481335 22.32198
5 d 7.945798 29.02631
6 c 9.631182 41.90519
7 c 9.348816 53.79478
8 a 4.559642 58.47290
9 d 9.876674 53.53151
10 c 12.955443 49.84759
,我需要创建一个函数,该函数以其名称访问任何给定变量,例如提取和报告,其平均值是'' :x
'(其中' x
'包含平均值)。到目前为止,我已经尝试过:
my.function <- function(df, variable) {
paste0("The mean is: ",
round(mean(df$variable),2))
}
但是当评估 my.function
在'我的test.df'中时,它表明这显然正在完成这项工作:
> my.function(test.df, variable_2)
[1] "The mean of the varibale is: NA"
所以我的问题是:
-
hoy我会调用变量函数论点中的名字?我知道有多种方法可以做到这一点,因为例如其他库,例如使用
variable_2
或“ variable_2”
,或者在需要多个变量时,要么列表没有报价的变量仅通过逗号将它们分开(variable_2,variable_3
如dplyr :: select()
),或者必须放置目标变量作为字符组(c(“ variable_2”,“ variable_3”)
如reshape2 :: melt()
)) - ) :我真的很喜欢使用需要多个变量的函数时,您可以按 tab ,并且显示了可用变量的列表(如
dplyr :: select()
例如)。构建自己的功能时如何获得此功能?
提前致谢! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们传递了列名称的未引用的参数,则使用
depars/替换
转换为字符串,并使用[[
而不是$
。另外,创建一个条件,以检查替代
是symbol
的值,然后使用deparse
,以便它可以通过引用和未引用的- 检验
如果我们想获得多个列的平均值,请使用
colmeans
并将变量作为字符向量- 测试
If we are passing unquoted argument for column names, then convert to string with
deparse/substitute
and use[[
instead of$
. Also, create a condition to check if the value fromsubstitute
issymbol
, then usedeparse
so that it can pass both quoted and unquoted-testing
If we want to get the mean of multiple columns, use
colMeans
and pass the variable as a character vector-testing
df $ nameofcolumn 。
您可以使用:示例:
可以在 r-devel/r-lang.html#索引“ rel =“ nofollow noreferrer”>索引
Instead of
df$nameOfColumn
, you can use:Example:
This can be found in the R Language Definition under Indexing