I tried to reproduce the result on the SO question: dplyr: How to apply do () to the group_by result?
Here are the data
person = c('Grace', 'Grace', 'Grace', 'Rob', 'Rob', 'Rob') foods = c('apple', 'banana', 'cucumber', 'spaghetti', 'cucumber', 'banana') eaten <- data.frame(person, foods, stringsAsFactors = FALSE)
The result that I tried to reproduce is as follows:
[[1]] [,1] [,2] [,3] [1,] "apple" "apple" "banana" [2,] "banana" "cucumber" "cucumber" [[2]] [,1] [,2] [,3] [1,] "spaghetti" "spaghetti" "cucumber" [2,] "cucumber" "banana" "banana"
The source code creating the result above, as below, no longer works:
> eaten %>% group_by(person) %>% do(function(x) combn(x$foods, m = 2)) Error: Results are not data frames at positions: 1, 2
I tried several ways to use the do () function to no avail.
> eaten %>% group_by(person) %>% do(combn(.$foods, m = 2)) Error: Results are not data frames at positions: 1, 2 > eaten %>% group_by(person) %>% do(.$foods, combn, m =2) Error: Arguments to do() must either be all named or all unnamed > eaten %>% group_by(person) %>% do((combn(.$foods, m=2))) Error: Results are not data frames at positions: 1, 2
Only one below seems to work with a warning:
> eaten %>% group_by(person) %>% do(as.data.frame(combn(.$foods, m = 2)))
Believe me, there should be a change in the behavior of do () in the new version. What are the changes? What is the correct idiom / way of using do ()? Thanks.
EDIT: the last dplyr is installed and run the code suggested by @hadley
packageVersion("dplyr") [1] '0.3.0.2' eaten %>% group_by(person) %>% do(x = combn(.$foods, m = 2))
EDIT2: need to extract column "x" as suggested by @hadley
eaten2 <- eaten %>% group_by(person) %>% do(x = combn(.$foods, m = 2)) eaten2[["x"]] # [[1]] # [,1] [,2] [,3] # [1,] "apple" "apple" "banana" # [2,] "banana" "cucumber" "cucumber" # # [[2]] # [,1] [,2] [,3] # [1,] "spaghetti" "spaghetti" "cucumber" # [2,] "cucumber" "banana" "banana"