【发布时间】:2018-09-20 02:44:46
【问题描述】:
我正在使用包含整数列表列的数据框。 list-column 中的每个元素对应于数据框中的一行,现在我想计算一下有多少个链接。
dput 包含一列 link_count,指示此数据样本的正确计数:
move link_count links
1 1 NA
2 0 1
3 1 NA
4 1 3
5 4 4
6 1 5
7 0 5, 6
8 2 5
9 0 8
10 0 5, 8
#dput results saved as `x`
x <- structure(list(move = 1:10, link_count = c(1, 0, 1, 1, 4, 1, 0, 2, 0, 0), links = list(NA_integer_, 1L, NA_integer_, 3L, 4L, 5L, 5:6, 5L, 8L, c(5L, 8L))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("move", "link count", "links"))
我找到了一个使用 left_join 的解决方案,但我认为可能有一个更优雅的解决方案可以使用 dplyr::mutate 或 purrr::map 工作流逐行完成。我希望可以在一系列管道中完成一些事情。
#This works, but is there a different way?
left_join(x,
x %>% unnest(links) %>% count(links),
by = c("move" = "links"))
【问题讨论】: