How can I add double quotes to a string taken from a vector in R?
I tried using paste but it doesn't work very well
kg_name=paste("\"",k_groups[i,],"\"")
lets say the value of k_groups[i,] is blah, so I'm looking for this result
"blah"
but the result is
"\" blah \""
To add single quotes to strings in an R data frame column, we can use paste0 function. This will cover the strings with single quotes from both the sides but we can add them at the initial or only at the last position.
If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
here 'a' is a char constant and is equal to the ASCII value of char a. Use double quote with strings as: char str[] = "foo"; here "foo" is a string literal.
Try shQuote
shQuote("blah")
# [1] "\"blah\""
The above will work on Windows. Use shQuote("blah", "cmd")
if you need it to work the same way giving double quotes on all operating systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With