I have a string:
words<-"Monday, Tuesday, Wednesday, Thursday,Friday"
and I only need add quotes to each word:
"Monday", "Tuesday", "Wednesday", "Thursday","Friday"
getting a length of five string.
I know there are many post about this topic, but I did´t find anything about it in R.
Many thanks.
How can I add double quotes to a string taken from a vector in R? If you do paste0, you will get rid of the spaces between the quotes and your string. The shQuote is used for escaping a command passed to a shell. It should not be used for just simple quoting. Show activity on this post. The above will work on Windows.
To append a string in R, use the paste () function. The paste () is a built-in function that concatenates or appends two or more strings. To concate strings in R, use the paste () function. The x is an R object.
If you want to display a string containing quotes without seeing the backslashes try cat (shQuote ("blah"), " ") . I am trying this way...but my prob is output is 'blah' not "blah". I need the same result. Is there any other way.
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. Almost, the problem is that I need exactly "blah" as a result not ""blah"".
Use gsub
words<-"Monday, Tuesday, Wednesday, Thursday,Friday"
cat(gsub("(\\w+)", '"\\1"', words))
# "Monday", "Tuesday", "Wednesday", "Thursday","Friday"
KISS....
cat(gsub("\\b", '"', words, perl=T))
#"Monday", "Tuesday", "Wednesday", "Thursday","Friday"
\\b
called word boundary which matches between a word character (A-Z,a-z,_,0-9) and a non-word character (not of A-Za-z0-9_) or vice-versa..
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