我正试图在R中添加一个向量,它是另两个列表的“超级集”。现在,我不认为我创建的列表是正确的,但希望这能为您提供一个示例,说明我要做的事情:
mygroups<-data.frame(number=c(1:5),members=c("a,b,c,d","e,f","b,c,d","e,f,g","c,d,a"))
mygroups2<- merge(mygroups, mygroups, by=NULL) %>%
filter(number.x!=number.y) %>%
mutate(setofall = paste(members.x,members.y, sep=","))
mygroups2现在,“七层坠落”并不完全是‘成员’的超集--它是一个直接的连接。例如,"a,b,c,d“和"b,c,d”被列为"a,b,c,d,d,b,c,d“,但对于这两种情况,”超集“应该只是"a,b,c,d”。
我搜索并发现了关于checking的问题--一个向量是否是另一个向量的超集,而不是寻找超集本身。
发布于 2020-06-27 18:15:23
这里有一个使用更多tidyverse的选项--您的超级集通常称为union
library(dplyr)
library(purrr)
mygroups<-data.frame(number=c(1:5),members=c("a,b,c,d","e,f","b,c,d","e,f,g","c,d,a"))
mygroups2<- merge(mygroups, mygroups, by=NULL) %>%
filter(number.x!=number.y) %>%
mutate(members.x = strsplit(members.x, ","),
members.y = strsplit(members.y, ","))
mygroups2$setofall <- purrr::map2(mygroups2$members.x, mygroups2$members.y, ~ union(.x, .y))
mygroups2
#> number.x members.x number.y members.y setofall
#> 1 2 e, f 1 a, b, c, d e, f, a, b, c, d
#> 2 3 b, c, d 1 a, b, c, d b, c, d, a
#> 3 4 e, f, g 1 a, b, c, d e, f, g, a, b, c, d
#> 4 5 c, d, a 1 a, b, c, d c, d, a, b
#> 5 1 a, b, c, d 2 e, f a, b, c, d, e, f
#> 6 3 b, c, d 2 e, f b, c, d, e, f
#> 7 4 e, f, g 2 e, f e, f, g
#> 8 5 c, d, a 2 e, f c, d, a, e, f
#> 9 1 a, b, c, d 3 b, c, d a, b, c, d
#> 10 2 e, f 3 b, c, d e, f, b, c, d
#> 11 4 e, f, g 3 b, c, d e, f, g, b, c, d
#> 12 5 c, d, a 3 b, c, d c, d, a, b
#> 13 1 a, b, c, d 4 e, f, g a, b, c, d, e, f, g
#> 14 2 e, f 4 e, f, g e, f, g
#> 15 3 b, c, d 4 e, f, g b, c, d, e, f, g
#> 16 5 c, d, a 4 e, f, g c, d, a, e, f, g
#> 17 1 a, b, c, d 5 c, d, a a, b, c, d
#> 18 2 e, f 5 c, d, a e, f, c, d, a
#> 19 3 b, c, d 5 c, d, a b, c, d, a
#> 20 4 e, f, g 5 c, d, a e, f, g, c, d, ahttps://stackoverflow.com/questions/62613000
复制相似问题