The dplyr::summarize()
function can apply arbitrary functions over the data, but it seems that function must return a scalar value. I'm curious if there is a reasonable way to handle functions that return a vector value without making multiple calls to the function.
dplyr::汇总()函数可以对数据应用任意函数,但函数似乎必须返回一个标量值。我很好奇,是否有一种合理的方法来处理返回向量值的函数,而不需要对函数进行多次调用。
Here's a somewhat silly minimal example. Consider a function that gives multiple values, such as:
这是一个有点愚蠢的小例子。考虑一个提供多个值的函数,例如:
f <- function(x,y){
coef(lm(x ~ y, data.frame(x=x,y=y)))
}
and data that looks like:
数据是这样的:
df <- data.frame(group=c('A','A','A','A','B','B','B','B','C','C','C','C'), x=rnorm(12,1,1), y=rnorm(12,1,1))
I'd like to do something like:
我想做一些类似的事情:
df %>%
group_by(group) %>%
summarise(f(x,y))
and get back a table that has 2 columns added for each of the returned values instead of the usual 1 column. Instead, this errors with: Expecting single value
然后返回一个表,其中为每个返回值添加了两个列,而不是通常的1列。相反,这个错误是:期望单个值。
Of course we can get multiple values from dlpyr::summarise()
by giving the function argument multiple times:
当然,我们可以从dlpyr中获得多个值::通过多次给出函数参数来总结():
f1 <- function(x,y) coef(lm(x ~ y, data.frame(x=x,y=y)))[[1]]
f2 <- function(x,y) coef(lm(x ~ y, data.frame(x=x,y=y)))[[2]]
df %>%
group_by(group) %>%
summarise(a = f1(x,y), b = f2(x,y))
This gives the desired output:
这给出了所需的输出:
group a b
1 A 1.7957245 -0.339992915
2 B 0.5283379 -0.004325209
3 C 1.0797647 -0.074393457
but coding in this way is ridiculously crude and ugly.
但是用这种方式编码是荒谬的粗糙和丑陋的。
data.table
handles this case more succinctly:
数据。表更简洁地处理这种情况:
dt <- as.data.table(df)
dt[, f(x,y), by="group"]
but creates an output that extend the table using additional rows instead of additional columns, resulting in an output that is both confusing and harder to work with:
但是,创建一个输出,它使用额外的行而不是额外的列来扩展表,从而产生一个既令人困惑又难以处理的输出:
group V1
1: A 1.795724536
2: A -0.339992915
3: B 0.528337890
4: B -0.004325209
5: C 1.079764710
6: C -0.074393457
Of course there are more classic apply
strategies we could use here,
当然这里有更多经典的应用策略,
sapply(levels(df$group), function(x) coef(lm(x~y, df[df$group == x, ])))
A B C
(Intercept) 1.7957245 0.528337890 1.07976471
y -0.3399929 -0.004325209 -0.07439346
but this sacrifices both the elegance and I suspect the speed of the grouping. In particular, note that we cannot use our pre-defined function f
in this case, but have to hard code the grouping into the function definition.
但这牺牲了优雅和我怀疑分组的速度。特别要注意的是,在本例中,我们不能使用预定义的函数f,但是必须硬编码分组到函数定义中。
Is there a dplyr
function for handling this case? If not, is there a more elegant way to handle this process of evaluating vector-valued functions over a data.frame by group?
是否有处理这种情况的dplyr函数?如果没有,那么是否有一种更优雅的方法来处理通过组对数据进行评估的方法?
2 个解决方案
#1
15
You could try do
你可以试着做
library(dplyr)
df %>%
group_by(group) %>%
do(setNames(data.frame(t(f(.$x, .$y))), letters[1:2]))
# group a b
#1 A 0.8983217 -0.04108092
#2 B 0.8945354 0.44905220
#3 C 1.2244023 -1.00715248
The output based on f1
and f2
are
基于f1和f2的输出是。
df %>%
group_by(group) %>%
summarise(a = f1(x,y), b = f2(x,y))
# group a b
#1 A 0.8983217 -0.04108092
#2 B 0.8945354 0.44905220
#3 C 1.2244023 -1.00715248
Update
If you are using data.table
, the option to get similar result is
如果你正在使用数据。表,得到类似结果的选项是。
library(data.table)
setnames(setDT(df)[, as.list(f(x,y)) , group], 2:3, c('a', 'b'))[]
#2
7
This is why I still love plyr::ddply()
:
这就是为什么我仍然喜欢plyr::ddply():
library(plyr)
f <- function(z) setNames(coef(lm(x ~ y, z)), c("a", "b"))
ddply(df, ~ group, f)
# group a b
# 1 A 0.5213133 0.04624656
# 2 B 0.3020656 0.01450137
# 3 C 0.2189537 0.22998823
#1
15
You could try do
你可以试着做
library(dplyr)
df %>%
group_by(group) %>%
do(setNames(data.frame(t(f(.$x, .$y))), letters[1:2]))
# group a b
#1 A 0.8983217 -0.04108092
#2 B 0.8945354 0.44905220
#3 C 1.2244023 -1.00715248
The output based on f1
and f2
are
基于f1和f2的输出是。
df %>%
group_by(group) %>%
summarise(a = f1(x,y), b = f2(x,y))
# group a b
#1 A 0.8983217 -0.04108092
#2 B 0.8945354 0.44905220
#3 C 1.2244023 -1.00715248
Update
If you are using data.table
, the option to get similar result is
如果你正在使用数据。表,得到类似结果的选项是。
library(data.table)
setnames(setDT(df)[, as.list(f(x,y)) , group], 2:3, c('a', 'b'))[]
#2
7
This is why I still love plyr::ddply()
:
这就是为什么我仍然喜欢plyr::ddply():
library(plyr)
f <- function(z) setNames(coef(lm(x ~ y, z)), c("a", "b"))
ddply(df, ~ group, f)
# group a b
# 1 A 0.5213133 0.04624656
# 2 B 0.3020656 0.01450137
# 3 C 0.2189537 0.22998823