如何融合包含列表变量的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
。
答案 0 :(得分:2)
我能想到的最佳选择是使用data.table
进行“融化”,如下所示:
library(data.table)
setDT(tbl)[, list(y = unlist(y)), by = x][]
# x y
# 1: A 1
# 2: A 2
# 3: B 3
# 4: B 4
答案 1 :(得分:1)
来自 tidyr 的unnest
函数(v.0.1.0.9000)是另一个可以正常运行的选项。
library(tidyr)
unnest(tbl, y)
Source: local data frame [4 x 2]
x y
1 A 1
2 A 2
3 B 3
4 B 4
您也可以将其直接添加到您的代码链中
data.frame( x = c("A", "A", "B", "B"), y = 1:4 ) %>%
tbl_df() %>%
group_by(x) %>%
do( y = .$y ) %>%
unnest(y)