提取列表元素并连接成r中的字符串

时间:2018-05-19 13:03:19

标签: r string list concatenation combinations

使用下面的变量列表我希望所有组合,将变量加入由“+”分隔的字符串

l_ALLVar_list <- c("a","b","c","d","z1","z2","z3") 

我有生成127种组合的代码

all_combos=do.call("c", lapply(seq_along(l_ALLVar_list), function(i) combn(l_ALLVar_list, i, FUN = list)))

并使用位置66作为示例

> all_combos[66]
[[1]]
[1] "a"  "b"  "c"  "z2"

我希望能够将索引66处的这些元素加入到字符串a + b + c + z2

我试过了

str_c(c(lol[66]),collapse=',')

但它又回来了

c(\"weight\", \"length\", \"wheel_base\", \"city_mpg\")

paste(all_combos[66], collapse = '')

再次生成相同的

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:3)

您可以在FUN中使用combn参数paste在一次通话中l_ALLVar_list的所有组合,从而无需使用all_combos列表。

unlist(lapply(seq_along(l_ALLVar_list), combn, x=l_ALLVar_list, paste, collapse="+"))
#  [1] "a"                "b"                "c"                "d"                "z1"              
#  [6] "z2"               "z3"               "a+b"              "a+c"              "a+d"             
# [11] "a+z1"             "a+z2"             "a+z3"             "b+c"              "b+d"             
# [16] "b+z1"             "b+z2"             "b+z3"             "c+d"              "c+z1"            
# [21] "c+z2"             "c+z3"             "d+z1"             "d+z2"             "d+z3"            
# [26] "z1+z2"            "z1+z3"            "z2+z3"            "a+b+c"            "a+b+d"           
# [31] "a+b+z1"           "a+b+z2"           "a+b+z3"           "a+c+d"            "a+c+z1"          
# [36] "a+c+z2"           "a+c+z3"           "a+d+z1"           "a+d+z2"           "a+d+z3"          
# [41] "a+z1+z2"          "a+z1+z3"          "a+z2+z3"          "b+c+d"            "b+c+z1"          
# [46] "b+c+z2"           "b+c+z3"           "b+d+z1"           "b+d+z2"           "b+d+z3"          
# [51] "b+z1+z2"          "b+z1+z3"          "b+z2+z3"          "c+d+z1"           "c+d+z2"          
# [56] "c+d+z3"           "c+z1+z2"          "c+z1+z3"          "c+z2+z3"          "d+z1+z2"         
# [61] "d+z1+z3"          "d+z2+z3"          "z1+z2+z3"         "a+b+c+d"          "a+b+c+z1"        
# [66] "a+b+c+z2"         "a+b+c+z3"         "a+b+d+z1"         "a+b+d+z2"         "a+b+d+z3"        
# [71] "a+b+z1+z2"        "a+b+z1+z3"        "a+b+z2+z3"        "a+c+d+z1"         "a+c+d+z2"        
# [76] "a+c+d+z3"         "a+c+z1+z2"        "a+c+z1+z3"        "a+c+z2+z3"        "a+d+z1+z2"       
# [81] "a+d+z1+z3"        "a+d+z2+z3"        "a+z1+z2+z3"       "b+c+d+z1"         "b+c+d+z2"        
# [86] "b+c+d+z3"         "b+c+z1+z2"        "b+c+z1+z3"        "b+c+z2+z3"        "b+d+z1+z2"       
# [91] "b+d+z1+z3"        "b+d+z2+z3"        "b+z1+z2+z3"       "c+d+z1+z2"        "c+d+z1+z3"       
# [96] "c+d+z2+z3"        "c+z1+z2+z3"       "d+z1+z2+z3"       "a+b+c+d+z1"       "a+b+c+d+z2"      
#[101] "a+b+c+d+z3"       "a+b+c+z1+z2"      "a+b+c+z1+z3"      "a+b+c+z2+z3"      "a+b+d+z1+z2"     
#[106] "a+b+d+z1+z3"      "a+b+d+z2+z3"      "a+b+z1+z2+z3"     "a+c+d+z1+z2"      "a+c+d+z1+z3"     
#[111] "a+c+d+z2+z3"      "a+c+z1+z2+z3"     "a+d+z1+z2+z3"     "b+c+d+z1+z2"      "b+c+d+z1+z3"     
#[116] "b+c+d+z2+z3"      "b+c+z1+z2+z3"     "b+d+z1+z2+z3"     "c+d+z1+z2+z3"     "a+b+c+d+z1+z2"   
#[121] "a+b+c+d+z1+z3"    "a+b+c+d+z2+z3"    "a+b+c+z1+z2+z3"   "a+b+d+z1+z2+z3"   "a+c+d+z1+z2+z3"  
#[126] "b+c+d+z1+z2+z3"   "a+b+c+d+z1+z2+z3"

答案 1 :(得分:2)

使用lapply对列表中的每个项目执行paste

result <- unlist(lapply(all_combos, 
  function(c) do.call(paste, c(as.list(c), sep="+"))))

> result[66:70]
[1] "a+b+c+z2" "a+b+c+z3" "a+b+d+z1" "a+b+d+z2" "a+b+d+z3"