1

I have

list1 <- c("A","B","C") 

and

list2 <- c(1,2,3)

And I want a joined list:

list3 = c("A_1", "B_2", "C_3").

I've looked everywhere but cannot find the answer. I've tried mapply, combn, permn... All did not work. Any help is appreciated!

TVV
  • 69
  • 6

1 Answers1

0

We can do this with paste. The input datasets are vectors

v1 <- paste(list1, list2, sep = "_")
dput(v1)
#c("A_1", "B_2", "C_3")

But, if they are list elements and wanted a vector as output

paste(unlist(list1), unlist(list2), sep = "_")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you so much! It solved my problem. And thanks for editing my question too. – TVV Jun 08 '17 at 10:48
  • Just another question, if I may. How about `list3 <- c("A_1","A_2","A_3", "B_1","B_2","B_3","C_1","C_2","C_3")` – TVV Jun 08 '17 at 10:50
  • 1
    I found the solution here https://stackoverflow.com/questions/16143700/pasting-two-vectors-with-combinations-of-all-vectors-elements. Thanks!! – TVV Jun 08 '17 at 11:06
  • @TVV I hope you don't have any difficulty – akrun Jun 08 '17 at 11:26