Home > Mobile >  paste some character vectors into one string in R
paste some character vectors into one string in R

Time:10-21

I have a character vector of for instance:

x = c("a","b","c")

and another string that goes like:

y = "The alphabet starts with"

When I do

paste(y,x)

I get

[1] "The alphabet starts with a" "The alphabet starts with b"
[3] "The alphabet starts with c"

But what I want to get is this:

[1] "The alphabet starts with a b c"

How do I manage to get this?

CodePudding user response:

Use paste twice. paste recycles a vector if the length of both vectors does not match. You should first create the vector "a b c", which is different from "a", "b", "c", and then paste it with y.

paste(y, paste(x, collapse = " "))
#[1] "The alphabet starts with a b c"

CodePudding user response:

Another alternative using R base:

> capture.output(cat(y,x))
[1] "The alphabet starts with a b c"
  • Related