dplyr:将分组数据传递给summary()中的函数
这是我经常遇到的dplyr问题。让我们考虑下面的代码:dplyr:将分组数据传递给summary()中的函数,r,dplyr,R,Dplyr,这是我经常遇到的dplyr问题。让我们考虑下面的代码: foo <- function (x, aux) {...} auxcols <- c("Sepal.Width", "Petal.Width") group_by(iris, Species) %>% summarize(f = foo(Sepal.Length, .[, auxcols])) foo您可以更改foo,使其将两列作为两个独立变量接收,如下所示: foo &l
foo <- function (x, aux) {...}
auxcols <- c("Sepal.Width", "Petal.Width")
group_by(iris, Species) %>%
summarize(f = foo(Sepal.Length, .[, auxcols]))
foo您可以更改foo
,使其将两列作为两个独立变量接收,如下所示:
foo <- function(length, sepal, petal) {
## if you really, really need to process it as a data.frame, just re-create it:
aux <- data.frame(Sepal.Width=sepal, Petal.width=petal)
}
group_by(iris, Species) %>% summarise(f= foo(Sepal.Length, Sepal.Width, Petal.Width))
foo@onyanbu提供了正确的解决方案
group_by(iris, Species) %>%
summarize(f = foo(Sepal.Length, cur_data()[, auxcols]))
非常简单。使用cur\u data()
而不是
@Onyambu Yes!你是我的英雄。非常感谢。不,那不可能,因为我事先不知道专栏。它们作为字符传递。我将调整描述。