我有一个列表,其元素是整数,如果这些元素至少共享一个值,我想累积这些元素。对于那些与其他元素不共享任何价值观的元素,我希望它们保持原样。这是我的样品日期:
x <- list(c(1, 2), c(1, 2, 3), c(2, 3, 4), c(3, 4, 5), c(4, 5, 8), c(6, 9, 7), 7, c(5, 8), 10, 11)
这是我想要的输出:
desired_reult <- list(c(1, 2, 3, 4, 5, 8),
c(6, 9, 7),
10,
11)
我想先用reduce
或accumulate
函数来做,purrr
但tidyverse
欢迎任何其他解决方案。到目前为止,我已经尝试过这个解决方案,但它只给了我一个union
,显然放弃了其余的:
x %>%
reduce(~ if(any(.x %in% .y)) union(.x, .y) else .x)
[1] 1 2 3 4 5 8
一般来说,我正在寻找一种将整数 (id) 与常见值(如某种聚类)进行分组的方法,但不幸的是,到目前为止,我的努力都是徒劳的。
非常感谢您提前提供帮助。
我怀疑有一套覆盖解决方案,但在此期间,这里有一个图形方法:
首先,让我们将整数向量转换为边列表,以便将其制成图形。我们可以使用expand.grid
.
library(igraph)
edgelist <- do.call(rbind,lapply(x,\(x)expand.grid(x,x)))
现在我们有一个两列的 data.frame,显示所有整数(一组边)之间的连接。
igraph::graph.data.frame
可以方便地从中制作图形。
从那里我们可以igraph::components
用来提取连接的组件。
g <- graph.data.frame(edgelist)
split(names(components(g)$membership),components(g)$membership)
#$`1`
#[1] "1" "2" "3" "4" "5" "8"
#$`2`
#[1] "6" "9" "7"
#$`3`
#[1] "10"
#$`4`
#[1] "11"
或者使用 Tidyverse:
library(dplyr); library(purrr)
map_dfr(x, ~expand.grid(.x,.x)) %>%
graph.data.frame() %>%
components() %>%
pluck(membership) %>%
stack() %>%
{split(as.numeric(.[,2]),.[,1])}
#$`1`
#[1] 1 2 3 4 5 6
#$`2`
#[1] 7 8 9
#$`3`
#[1] 10
#$`4`
#[1] 11
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句