13

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.

Tomás Navarro
  • 160
  • 1
  • 1
  • 7

3 Answers3

14

We can split the words by , to get a list output. We loop through sapply , dQuote the elements and then paste it together with toString which is a wrapper for paste(..., collapse=', ').

sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x)))
#[1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"

If we need to change the fancy quotes, add FALSE in dQuote

sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x, FALSE)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 3
    Thanks, @akrun. How would you change the fancy quotes to straight quotes? I tried your code and set "options(useFancyQuotes = FALSE)", but it inserted slashes everywhere: `"\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\""` – Nova Feb 18 '16 at 18:07
  • 1
    @Nova By default, the quotes are escaped. But, if it is for printing purpose wrap with `cat` and it will print without the slashes. – akrun Feb 18 '16 at 18:16
  • 2
    Adding that `dQuote` = "double quote"; there's also `sQuote` for single quotes. `dQuote(x, FALSE)` forces the vanilla `"` (resp, `'`) to be used (as opposed to possibly "fancy" angled quotes). – MichaelChirico Apr 29 '20 at 04:28
  • I've found a difference between Windows and Mac in this regard (all other settings, such as UTF-8, remaining identical, as far as I know). Only on Mac did I need to use the `FALSE` argument suggested by @MichaelChirico (thank you btw!), lest I got 'Error in parse... unexpected input'. – Pablo Bernabeu Jun 05 '20 at 20:37
  • 1
    @PabloBernabeu This was a solution posted in 2015 on a different version of R. Not clear whether you want to downvote an answer based on this behavior now. Anyway, it is working fine on `Mac` for me with `R 4.0.0` `sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x)))# [1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"` – akrun Jun 05 '20 at 20:39
  • @akrun Thanks for the follow-up. This answer actually worked wonders for me on Windows-R-[Shiny](https://shiny.rstudio.com/), so thank you! I've just happened to run my code on a Mac instead, and I did spend a few hours in the rabbit hole until I happily landed back here around the comments. So, I wouldn't downvote the answer; perhaps just add the `FALSE` argument? If you like, please feel free to add it yourself. The code also works for me on Mac-RStudio, but just not in a Shiny app (whereas it does when the app is launched on Windows). – Pablo Bernabeu Jun 05 '20 at 20:46
  • @PabloBernabeu. I am using MacOS catalin and it is working fine for me with R 4.0 – akrun Jun 05 '20 at 20:47
  • Correct--thanks. My issue seems to be specific to [Shiny](https://shiny.rstudio.com/) run on Mac. – Pablo Bernabeu Jun 05 '20 at 20:57
9

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..

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
4

strsplit splits string by comma, and sub removes white spaces.

paste(dQuote(sub(" ","",unlist(strsplit(words,split = ",")))),collapse = ", ")

[1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"
vck
  • 827
  • 5
  • 10