如何在 R 中创建一个字符串向量中元素的无序组合列表?


元素的无序组合意味着以不形成任何特定顺序的方式对值进行组合。例如,如果我们有三个值一、二、三,则它们可以按无序的方式排列如下 −

"one"
"two" "three"
"one" "two"
"one" three"
"two" "three"
"one" "two" "three"

示例

 实时演示

x<-c("India","China","Russia","Canada") unlist(lapply(seq_along(x),combn,x=x,simplify=FALSE),recursive=FALSE)

输出

[[1]]
[1] "India"
[[2]]
[1] "China"
[[3]]
[1] "Russia"
[[4]]
[1] "Canada"
[[5]]
[1] "India" "China"
[[6]]
[1] "India" "Russia"
[[7]]
[1] "India" "Canada"
[[8]]
[1] "China" "Russia"
[[9]]
[1] "China" "Canada"
[[10]]
[1] "Russia" "Canada"
[[11]]
[1] "India" "China" "Russia"
[[12]]
[1] "India" "China" "Canada"
[[13]]
[1] "India" "Russia" "Canada"
[[14]]
[1] "China" "Russia" "Canada"
[[15]]
[1] "India" "China" "Russia" "Canada"

示例

 实时演示

y<-c("Winter","Rainy","Summer") unlist(lapply(seq_along(y),combn,x=y,simplify=FALSE),recursive=FALSE)

输出

[[1]]
[1] "Winter"
[[2]]
[1] "Rainy"
[[3]]
[1] "Summer"
[[4]]
[1] "Winter" "Rainy"
[[5]]
[1] "Winter" "Summer"
[[6]]
[1] "Rainy" "Summer"
[[7]]
[1] "Winter" "Rainy" "Summer"

更新时间: 2020 年 10 月 14 日

135 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告