找到矢量的所有可能组合

时间:2016-03-09 07:48:10

标签: r string combinations

我有一个字符串v = c("abc", "csb", "csfd", "dds", "edds")。 我想使用向量v的元素生成小于或等于长度n的所有可能组合。

我尝试了combn函数,结果是这样的结果

>>combn(v,2)

     [,1]  [,2]   [,3]  [,4]   [,5]   [,6]  [,7]   [,8]   [,9]   [,10] 
[1,] "abc" "abc"  "abc" "abc"  "csb"  "csb" "csb"  "csfd" "csfd" "dds" 
[2,] "csb" "csfd" "dds" "edds" "csfd" "dds" "edds" "dds"  "edds" "edds"

但我想要复制字符串和低学位术语的组合。例如,如果n = 2,那么输出应该像

     [,1]  [,2]   [,3]  [,4]   [,5]   [,6]  [,7]   [,8]   [,9]   [,10] 
[1,] "abc" "csb" "csfd" "dds" "edds" "abc" "abc"  "abc"  "abc"  " csb"  
[2,] ""      ""     ""    ""    ""   "abc" "csb" "csfd"  "edds" "edds"

和其他条款。

1 个答案:

答案 0 :(得分:2)

我们循环通过' n'使用lapply,指定' m'在combn作为循环值,使用combn获取data.frame输出的转置,转换为rbind然后list rbindlist的元素(来自data.tablefill=TRUE

library(data.table)
n <- 2
lst <- lapply(seq(n), function(i) as.data.frame(t(combn(v,i))))
rbindlist(lst, fill=TRUE)
#      V1   V2
# 1:  abc   NA
# 2:  csb   NA
# 3: csfd   NA
# 4:  dds   NA
# 5: edds   NA
# 6:  abc  csb
# 7:  abc csfd
# 8:  abc  dds
# 9:  abc edds
#10:  csb csfd
#11:  csb  dds
#12:  csb edds
#13: csfd  dds
#14: csfd edds
#15:  dds edds

如果我们需要所有组合,请使用expand.grid

lst <- lapply(seq(n), function(i) expand.grid(rep(list(v), i)))
rbindlist(lst, fill=TRUE)
#    Var1 Var2
# 1:  abc   NA
# 2:  csb   NA
# 3: csfd   NA
# 4:  dds   NA
# 5: edds   NA
# 6:  abc  abc
# 7:  csb  abc
# 8: csfd  abc
# 9:  dds  abc
#10: edds  abc
#11:  abc  csb
#12:  csb  csb
#13: csfd  csb
#14:  dds  csb
#15: edds  csb
#16:  abc csfd
#17:  csb csfd
#18: csfd csfd
#19:  dds csfd
#20: edds csfd
#21:  abc  dds
#22:  csb  dds
#23: csfd  dds
#24:  dds  dds
#25: edds  dds
#26:  abc edds
#27:  csb edds
#28: csfd edds
#29:  dds edds
#30: edds edds