Creating a comma separated vector

Viewed 106540

I have a numeric vector, one, which I'm trying to turn into a character vector where each element is separated by commas.

> one = c(1:5)
> paste(as.character(one), collapse=", ")
[1] "1, 2, 3, 4, 5"
> paste(as.character(one), sep="' '", collapse=", ")
[1] "1, 2, 3, 4, 5"

However, I want the output to look like:

"1", "2", "3", "4", "5" 

Am I missing some parameter from the paste function? Help!?

6 Answers

Something similar with toString

toString(paste0("'",1:10,"'") )

Just to add on to Noah's answer if you want to use the paste function:

paste(shQuote(one, type="sh"), collapse=", ")

Should give you:

[1] '1','2','3','4','5'
Related