Search code examples
rapplypaste

combining elements in a list of lists in R


I have a list called samples_ID with 116 vectors, each vectors has three elements like these:

"11"         "GT20-16829" "S27"

I wanna keep the 116 vectors, but combine the elements to a single element like this

"11_GT20-16829_S27"

I tried something like this

samples_ID_ <- paste(samples_ID, collapse = "_")

it returns a single vector, below is just a part of it:

..._c(\"33\", \"GT20-16846\", \"S24\")_c(\"33\", \"GT20-18142\", \"S72\")_c(\"34\", \"GT20-16819\", \"S50\")_c...

What am I doing wrong? Can you help me please? Thanks


Solution

  • Another base R option using paste

    do.call(paste, c(data.frame(t(list2DF(samples_ID))), sep = "_"))
    

    or

    do.call(paste, data.frame(do.call(rbind, samples_ID)), sep = "_"))