0

I have dataframe df, and want a row with collapsed column values with quotes. Output should be "'a','b','c'", but I have "a,b,c". How can I get the right output?

df<-data.frame(
  a=c("a","b","c"),
  number=c(1,2,3)
)
paste(df$a,collapse = ",")
Priit Mets
  • 465
  • 2
  • 14
  • 1
    `paste(sQuote(df$a),collapse=",")` or `paste(paste0("'",df$a,"'"),collapse=",")`, though the latter will not properly "escape" embedded single quotes. – r2evans Mar 19 '21 at 14:36
  • Do you need this output for an SQL query? Because `dbplyr` could easily solve this issue for you then. – hannes101 Mar 19 '21 at 14:58

1 Answers1

0

Try this;

paste(
    "'",c("A", "B", "C"), "'", sep = "", collapse = ","
)

It gives the following output,

"'A','B','C'"
Serkan
  • 1,855
  • 6
  • 20
  • thank you, but If I need to place "\' " instead of simply "'", what should I do? i tried paste("\'"...) however got only ', tried paste("\\'"...) and got "\\'), Output should be like " \'A,\',\'B\',\'C\' " – Priit Mets Mar 19 '21 at 15:52