将data.frame列名称传递给函数 [英] Pass a data.frame column name to a function

查看:107
本文介绍了将data.frame列名称传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数来接受一个data.frame( x )和一个它。该函数对x执行一些计算,之后返回另一个data.frame。我坚持使用最佳实践方法将列名称传递给函数。

I'm trying to write a function to accept a data.frame (x) and a column from it. The function performs some calculations on x and later returns another data.frame. I'm stuck on the best-practices method to pass the column name to the function.

两个最小例子 fun1 fun2 以下产生所需的结果,可以使用<$ c执行 x $ column $ c> max()为例。但是,两者都依赖于(至少对我)不满意的

The two minimal examples fun1 and fun2 below produce the desired result, being able to perform operations on x$column, using max() as an example. However, both rely on the seemingly (at least to me) inelegant


  1. 调用 substitute()可能 eval()

  2. 需要将列名称作为字符向量传递。

  1. call to substitute() and possibly eval()
  2. the need to pass the column name as a character vector.

fun1 <- function(x, column){
  do.call("max", list(substitute(x[a], list(a = column))))
}

fun2 <- function(x, column){
  max(eval((substitute(x[a], list(a = column)))))
}

df <- data.frame(B = rnorm(10))
fun1(df, "B")
fun2(df, "B")

我希望能够将函数调用为 fun(df,B)例如。其他选项我已经考虑过但没有尝试过:

I would like to be able to call the function as fun(df, B), for example. Other options I have considered but have not tried:


  • 作为整数的列号。我认为这将避免 substitute()

  • with(x,get(column)),但即使它有效,我也可以接受认为这将仍然需要替代

  • 使用 formula() match.call(),我没有太多经验。

  • Pass column as an integer of the column number. I think this would avoid substitute(). Ideally, the function could accept either.
  • with(x, get(column)), but, even if it works, I think this would still require substitute
  • Make use of formula() and match.call(), neither of which I have much experience with.

Subquestion do.call()首选超过 eval()


Subquestion: Is do.call() preferred over eval()?

推荐答案

您可以直接使用列名:

df <- data.frame(A=1:10, B=2:11, C=3:12)
fun1 <- function(x, column){
  max(x[,column])
}
fun1(df, "B")
fun1(df, c("B","A"))

没有必要使用替代,eval等。

There's no need to use substitute, eval, etc.

您甚至可以将所需的功能作为参数传递:

You can even pass the desired function as a parameter:

fun1 <- function(x, column, fn) {
  fn(x[,column])
}
fun1(df, "B", max)

Alt在本质上,使用 [[也可以一次选择一列):

Alternatively, using [[ also works for selecting a single column at a time:

df <- data.frame(A=1:10, B=2:11, C=3:12)
fun1 <- function(x, column){
  max(x[[column]])
}
fun1(df, "B")

这篇关于将data.frame列名称传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆