I'd like to use dplyr to group a table by one column, then apply a function to the set of values in the second column of each group.
For instance, in the code example below, I'd like to return all of the 2-item combinations of foods eaten by each person. I cannot figure out how to properly supply the function with the proper column (foods) in the do()
function.
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 ) )
Note that the example code in ?do
fails on my machine
mods <- do(carriers, failwith(NULL, lm), formula = ArrDelay ~ date)
See Question&Answers more detail:os