我想使用 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)