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

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

问题描述

我正在尝试编写一个函数来接受一个 data.frame (x) 和一个 column .该函数对 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.

下面两个最小的例子fun1fun2 产生了想要的结果,能够对x$column 执行操作,使用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. 需要将列名作为字符向量传递.

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:

  • column 作为列号的整数传递.我认为这会避免 substitute().理想情况下,该函数可以接受其中之一.
  • with(x, get(column)),但是,即使它有效,我认为这仍然需要 substitute
  • 利用 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.

子问题:do.call() 是否优于 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"))

无需使用替代、评估等

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

You can even pass the desired function as a parameter:

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

或者,使用 [[ 也适用于一次选择一列:

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天全站免登陆