【发布时间】:2014-08-13 15:19:46
【问题描述】:
如何融化包含列表变量的tbl_df?我只是在寻找的倒数
library( dplyr )
tbl <- data.frame( x = c("A", "A", "B", "B"), y = 1:4 ) %>%
tbl_df() %>%
group_by(x) %>%
do( y = .$y )
tbl
Source: local data frame [2 x 2]
Groups: <by row>
x y
1 A <int[2]>
2 B <int[2]>
我想到了类似的东西
tbl %>%
mutate( y = unlist(y) )
Error: incompatible size (2), expecting 1 (the group size) or 1
library( reshape2 )
melt( tbl, id.vars = "x" )
Error: Can't melt data.frames with non-atomic columns
编辑这是sessionInfo()
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] graphics grDevices datasets stats utils methods base
other attached packages:
[1] dplyr_0.2 ggplot2_1.0.0 stringr_0.6.2 reshape2_1.4 plyr_1.8.1
loaded via a namespace (and not attached):
[1] assertthat_0.1 colorspace_1.2-4 digest_0.6.4 grid_3.1.0 gtable_0.1.2 magrittr_1.0.1 MASS_7.3-33 munsell_0.4.2
[9] parallel_3.1.0 proto_0.3-10 Rcpp_0.11.2 scales_0.2.4 tools_3.1.0
dplyr在plyr之后加载。
【问题讨论】:
-
@AnandaMahto 我添加了
sessionInfo。它也是 0.2 版。 -
您不能取消列出
y并将其放回tbl,因为它是当前大小的两倍(从 2 到 4),因此我能想到的唯一方法是 @987654331 @ -
tbl %>% do(data.frame(x = .$x, y = .$y))怎么样? -
我也尝试了基于组的
do,它也有效,但看起来不太好:tbl %>% group_by(x) %>% do(data.frame(y = unlist(.$y))) -
@aosmith 我没有对这个进行基准测试,所以这只是猜测,但我担心这个
do(data.frame(...))的事情比做一个简单的mutate( trallala )要慢得多。这就是我所说的不是很优雅。但仍然非常感谢您的建议。