2

I am working with the R programming language. I have a data frame called "a" with a single column called "V1". Each entry in "a" is a URL link in the following format:

 1 https:blah-blah-blah.com/item/123/index.do
 2 https:blah-blah-blah.com/item/124/index.do
 3 https:blah-blah-blah.com/item/125/index.do

I am trying to add double quotes (i.e " ") in front of and behind each of these items, so they look like this:

 1 "https:blah-blah-blah.com/item/123/index.do"
 2" https:blah-blah-blah.com/item/124/index.do"
 3 "https:blah-blah-blah.com/item/125/index.do"

Using this previous stackoverflow post (How to add quotes around each word in a string in R? ) , I tried the following code:

new_file = cat(gsub("\\b", '"',a$V1, perl=T))

and

new_file = cat(gsub("(\\w+)", '"\\1"', a$V1))

But these are not producing the intended results. Can someone please show me what I am doing wrong?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    Your data seems to already be in a vector, use `dQuote(a$V1, q=FALSE)`. For example: `cat(dQuote(letters[1:3], q=FALSE))` (note I used `cat()` so R wouldn't escape the quotes when printing to the console). No need to bother with `gsub()`. But why exactly are you trying to wrap these in quotes? Is it just a formatting thing? – MrFlick Apr 11 '21 at 03:20
  • Does `class(a$V1)` return "character"? If so the strings are quoted, but the quotes are not always printed. To just add quotes when you print, e.g. `print(a, quote=TRUE)` or `print(a$V1, quote=TRUE`). They will also be quoted when you write the values to a file: `write.csv(a)`. – dcarlson Apr 11 '21 at 04:30
  • I'm not sure why you should want this, but you can try something like this `a <- a %>% mutate(V1 = paste0('"', V1, '"'))`. You need l to load `dplyr` package first. – rdatasculptor Apr 11 '21 at 05:31

2 Answers2

2

Fixing your approach:

a <-  data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
                        "https:blah-blah-blah.com/item/124/index.do",
                        "https:blah-blah-blah.com/item/125/index.do"))

a$V1 <- gsub("^", "\"", a$V1)
a$V1 <- gsub("$", "\"", a$V1)
a
#>                                             V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"

The ^ character indicates the beginning of the string; and $ indicates it’s end.

@MrFlicks solution works, too

a <-  data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
                        "https:blah-blah-blah.com/item/124/index.do",
                        "https:blah-blah-blah.com/item/125/index.do"))
a$V1 <- dQuote(a$V1, q=FALSE)
a
#>                                             V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"

Created on 2021-04-11 by the reprex package (v2.0.0)

Jan
  • 4,974
  • 3
  • 26
  • 43
2

In my humble opinion paste0 should do the job.

  • Actually " & ' do the same job but act differently when used simultaneously. See the two cases below-

case-1

a <-  data.frame(V1 = c('https:blah-blah-blah.com/item/123/index.do',
                        'https:blah-blah-blah.com/item/124/index.do',
                        'https:blah-blah-blah.com/item/125/index.do'))

a
                                          V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do

a$V1 <- paste0('"', a$V1, '"')
a

                                            V1
1 "https:blah-blah-blah.com/item/123/index.do"
2 "https:blah-blah-blah.com/item/124/index.do"
3 "https:blah-blah-blah.com/item/125/index.do"

case-2

a <-  data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
                        "https:blah-blah-blah.com/item/124/index.do",
                        "https:blah-blah-blah.com/item/125/index.do"))
 
a
                                          V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do

a$V1 <- paste0("'", a$V1, "'")
a
                                            V1
1 'https:blah-blah-blah.com/item/123/index.do'
2 'https:blah-blah-blah.com/item/124/index.do'
3 'https:blah-blah-blah.com/item/125/index.do'
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45