I have a list of lists of characters. For example:
l <- list(list("A"),list("B"),list("C","D"))
So as you can see some elements are lists of length > 1.
I want to convert this list of lists to a character vector, but I'd like the lists with length > 1 to appear as a single element in the character vector.
the unlist
function does not achieve that but rather:
> unlist(l)
[1] "A" "B" "C" "D"
Is there anything faster than:
sapply(l,function(x) paste(unlist(x),collapse=""))
To get my desired result:
"A" "B" "CD"
You can skip the unlist step. You already figured out that paste0
needs collapse = TRUE
to "bind" sequential elements of a vector together:
> sapply( l, paste0, collapse="")
[1] "A" "B" "CD"