Combine list components to a vector

Viewed 53

Suppose, I have a list:

l = list(c("a", "b", "c"), c("d", "e", "f"))

[[1]]
[1] "a" "b" "c"

[[2]]
[1] "d" "e" "f"

I want to get a vector.

"ad" "be" "cf"

I can convert the list to a matrix, e.g.,sapply(l, c), and then concatenate columns, but, perhaps, there is an easier way.

2 Answers

We can use Reduce with paste0

Reduce(paste0, l)
[1] "ad" "be" "cf"

Or with do.call

do.call(paste0, l)
[1] "ad" "be" "cf"

Here is another option

> apply(list2DF(l), 1, paste0, collapse = "")
[1] "ad" "be" "cf"
Related