Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding double quotes to string in R

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 \""
like image 857
Atirag Avatar asked Feb 13 '15 00:02

Atirag


People also ask

How do you put quotes around a string in R?

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.

How do you insert a double quote into a quoted string?

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.

Can char have double quotes?

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.


Video Answer


1 Answers

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.

like image 178
G. Grothendieck Avatar answered Sep 24 '22 12:09

G. Grothendieck