Wytamma Wirth
Wytamma Wirth

Reputation: 563

Paste a vector of vectors in R

I have a vector of vectors e.g.

c(c('a','b'), c('a','b'))

I want to join the sub-vectors with paste e.g.

lapply(c(c('a','b'), c('a','b')), paste)

which that results in

[[1]]
[1] "a"

[[2]]
[1] "b"

[[3]]
[1] "a"

[[4]]
[1] "b"

however I want the result to be

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

[[2]]
[1] "a b"

Upvotes: 0

Views: 39

Answers (1)

deschen
deschen

Reputation: 10996

You need a list of vectors. And in the paste function, you want to collapse the elements, so you can do:

lapply(list(c('a','b'), c('a','b')), paste, collapse = " ")

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

[[2]]
[1] "a b"

Upvotes: 1

Related Questions