Serhii
Serhii

Reputation: 422

Combine list components to a vector

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.

Upvotes: 2

Views: 59

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101099

Here is another option

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

Upvotes: 3

akrun
akrun

Reputation: 886948

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"

Upvotes: 4

Related Questions