Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

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!?

1 Answer

0 votes
by
edited by

To create a comma-separated vector, you can use the shQuote function from the base package as follows:

one = c(1:5)

> cat(paste(shQuote(one, type="cmd"), collapse=", "))

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

type="cmd" argument provides escaped quotes, which is what is actually useful for most contexts, but if you really want to display it somewhere with unescaped quotes, then the cat function provides that.

Browse Categories

...