我正在使用一个包含整数列表列的数据框。列表列中的每个元素对应于数据框中的一行,现在我想计算有多少个链接。
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"))
最佳答案
base R
选项是。这里,缺少的元素是 NA
而不是 0
s
as.numeric(table(unlist(x$links))[as.character(x$move)])
关于r - 逐行计算列表列中的实例数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52416817/