dplyr 0.3.0.9000如何正确使用do() [英] dplyr 0.3.0.9000 how to use do() correctly

查看:120
本文介绍了dplyr 0.3.0.9000如何正确使用do()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在SO问题上重现结果:
dplyr:如何在group_by的结果上应用do()?

Tried to reproduce the result on a SO question: dplyr: How to apply do() on result of group_by?

这里是数据

person = c('Grace', 'Grace', 'Grace', 'Rob', 'Rob', 'Rob')
foods = c('apple', 'banana', 'cucumber', 'spaghetti', 'cucumber', 'banana')
eaten <- data.frame(person, foods, stringsAsFactors = FALSE)

我尝试复制的结果是:

[[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 original code producing the result above is as below which 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

尝试使用do()函数无效的几种方法。

Tried several ways to use 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

似乎只有下面的一个可以使用警告消息:

Seems only the one below works with warning message though:

> eaten %>% group_by(person) %>% do(as.data.frame(combn(.$foods, m = 2)))
#   person        V1        V2       V3
# 1  Grace     apple     apple   banana
# 2  Grace    banana  cucumber cucumber
# 3    Rob spaghetti spaghetti cucumber
# 4    Rob  cucumber    banana   banana
# Warning messages:
# 1: In rbind_all(out[[1]]) : Unequal factor levels: coercing to character
# 2: In rbind_all(out[[1]]) : Unequal factor levels: coercing to character

相信在新版本下do()的行为必须改变。有什么变化?什么是正确的成语/使用do()的方法?谢谢。

Believe there must a change on the behavior of do() under new version. What are the changes? What is the right idiom / way to use do()? Thanks.

packageVersion("dplyr")
[1] ‘0.3.0.2’

eaten %>% group_by(person) %>% do(x = combn(.$foods, m = 2))
# Source: local data frame [2 x 2]
# Groups: <by row>
#   
#   person          x
# 1  Grace <chr[2,3]>
# 2    Rob <chr[2,3]>



EDIT2:需要根据@hadley

的建议提取列x

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" 


推荐答案

移动EDIT2在Q中回答以关闭问题:

Move EDIT2 in Q to answer to close the question:

对于最新的 dplyr 0.3.0.2+,需要按照@hadley的建议提取列x

For latest dplyr 0.3.0.2+, 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

这篇关于dplyr 0.3.0.9000如何正确使用do()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆