【发布时间】:2014-03-04 20:34:41
【问题描述】:
我想使用 dplyr 按一列对表进行分组,然后将函数应用于每组第二列中的一组值。
例如,在下面的代码示例中,我想返回每个人所吃食物的所有 2 项组合。我无法弄清楚如何在do() 函数中正确地为函数提供正确的列(食物)。
library(dplyr)
person = c( 'Grace', 'Grace', 'Grace', 'Rob', 'Rob', 'Rob' )
foods = c( 'apple', 'banana', 'cucumber', 'spaghetti', 'cucumber', 'banana' )
eaten = data.frame(person, foods)
by_person = group_by(eaten, person)
# How to do this?
do( by_person, combn( x = foods, m = 2 ) )
请注意,?do 中的示例代码在我的机器上失败了
mods <- do(carriers, failwith(NULL, lm), formula = ArrDelay ~ date)
【问题讨论】:
-
在闲逛之后,我看到这个问题被重新提出并回答了
dplyrstackoverflow.com/q/26336180/8400969的演变