dplyr:如何对group_by的结果应用do()? [英] dplyr: How to apply do() on result of group_by?
问题描述
我想使用dplyr将表格分组一列,然后将函数应用于每个组的第二列中的值集合。
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.
例如,在下面的代码示例中,我想返回每个人吃的食物的所有2项组合。我不知道如何正确地在 do()
函数中提供适当的列(食品)功能。
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)
推荐答案
让我们定义 eaten
如下:
eaten <- data.frame(person, foods, stringsAsFactors = FALSE)
1 )
1) Then try this:
eaten %.% group_by(person) %.% do(function(x) combn(x$foods, m = 2))
给:
[[1]]
[,1] [,2] [,3]
[1,] "apple" "apple" "banana"
[2,] "banana" "cucumber" "cucumber"
[[2]]
[,1] [,2] [,3]
[1,] "spaghetti" "spaghetti" "cucumber"
[2,] "cucumber" "banana" "banana"
2)为了能够在@Hadley在评论中描述的内容,而不用等待未来版本的dplyr,可以尝试这个 do2
此处:
2) To be able to do something near to what @Hadley describes in the comments without waiting for a future version of dplyr try this where do2
is found here:
library(gsubfn)
eaten %.% group_by(person) %.% fn$do2(~ combn(.$foods, m = 2))
给:
$Grace
[,1] [,2] [,3]
[1,] "apple" "apple" "banana"
[2,] "banana" "cucumber" "cucumber"
$Rob
[,1] [,2] [,3]
[1,] "spaghetti" "spaghetti" "cucumber"
[2,] "cucumber" "banana" "banana"
注意:在帮助文件中给出代码的问题的最后一行也对我失败。这个变体对我有用: do(jan,lm,formula = ArrDelay〜date)
。
Note: The last line of the question giving the code in the help file also fails for me. This variation of it works for me: do(jan, lm, formula = ArrDelay ~ date)
.
这篇关于dplyr:如何对group_by的结果应用do()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!