Beans On Toast
Beans On Toast

Reputation: 1091

convert double quotes to single quotes in string?

In R, if you have the following string, which is a vector and each element is in double quotes:

values <- c("hello","mr") 

How can one go about creating the following string instead?

"'hello','mr'"

As in how do i add in those single quotes, but ensure the entire string is one sentence wrapped in double quotes?

I can hard code this with paste0 but it's not the same thing.

Upvotes: 1

Views: 800

Answers (2)

awaji98
awaji98

Reputation: 685

Simple stringr solution:

library(stringr)
str_c("'",values,"'", collapse = ",")

Upvotes: 6

Ma&#235;l
Ma&#235;l

Reputation: 52389

With sQuote and paste:

paste(sQuote(values, q = FALSE), collapse = ",")
# [1] "'hello','mr'"

Upvotes: 1

Related Questions