Ab Bennett
Ab Bennett

Reputation: 1432

extract list elements and concatenate into a string in r

using the variable list below I want to for all combinations, join the variables into a string seperated by "+"

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

I have the code to generate the 127 combinations

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

and using position 66 as an example

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

I want to be able to join the elements of these at index 66 into the string a+b+c+z2

I have tried

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

but it comes back as

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

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

produces the same again

any help would be appreciated

Upvotes: 3

Views: 251

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99331

You can use the FUN argument in combn to paste all the combinations of l_ALLVar_list in one call, eliminating the need for your all_combos list.

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"

Upvotes: 3

J. Win.
J. Win.

Reputation: 6771

Use lapply to do paste for each item in your list:

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"

Upvotes: 2

Related Questions