bvowe
bvowe

Reputation: 3384

R Create Custom List

paste0("a",1:5)
"a1" "a2" "a3" "a4" "a5"

I want this output: "a1", "a2", "a3", "a4", "a5"

I tried paste0("a",1:5, sep=",") but also paste0("a",1:5, collapse=",") but these did not do it.

Upvotes: 0

Views: 58

Answers (2)

akrun
akrun

Reputation: 887213

We can also use str_c from stringr

library(stringr)
str_c('"a', 1:5, '"', collapse=",")
#[1] "\"a1\",\"a2\",\"a3\",\"a4\",\"a5\""

Or with base R with dQuote and paste

dQuote(paste0('a', 1:5), q = FALSE)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389012

You could use

paste0('"a', 1:5, '"', collapse = ',')
#[1] "\"a1\",\"a2\",\"a3\",\"a4\",\"a5\""

R uses backslashes to print quotes, to see the actual string use cat

cat(paste0('"a', 1:5, '"', collapse = ','))
#"a1","a2","a3","a4","a5"

Upvotes: 1

Related Questions