10

I need to remove \" from a vector. This is my data:

data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1803224&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-linux-security-masterclass-3-in-1%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1848638&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fmastering-kali-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1426684&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Finformation-gathering-with-kali-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1628300&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-switchblade%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1615700&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fadministrador-de-sistemas-junior-en-windows-server-y-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.809770&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flearn-bash-shell-in-linux-for-beginners-lite%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.574388&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fhow-to-install-linux-ubuntu-server%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1436610&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fcentos-and-ubuntu-managing-packages%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1771266&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-foundation-certified-system-administrator-exam%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1734052&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-server-security%2F"
)

As you can see, every object starts with \". How can I specifically remove these characters and leave the links?

zx8754
  • 52,746
  • 12
  • 114
  • 209
antecessor
  • 2,688
  • 6
  • 29
  • 61

8 Answers8

16

You can try this. Note that what you actually want is to remove \", not "\ (as proposed in the unedited version of your question). The first " you need to represent each element in the character.

gsub('[\"]', '', data)
milan
  • 4,782
  • 2
  • 21
  • 39
12

Or we can just use '"' on the pattern

gsub('"', "", data)
akrun
  • 874,273
  • 37
  • 540
  • 662
7

If it is always 1st character then just use substring:

substring(data, 2)

This should be faster than any regex solution.

data <- rep(data, 1000)

microbenchmark::microbenchmark(
  a = substring(data, 2),  
  b = gsub("\"", "", data, fixed = TRUE),
  c = gsub('"', "", data),
  d = gsub('[\"]', '', data),
  e = stringr::str_replace(data, '[\"]', ''),
  f = gsub("^.","",data)
  )
# Unit: milliseconds
# expr       min        lq      mean    median        uq       max neval
#    a  2.835013  2.849838  2.933796  2.857393  2.900301  4.446956   100
#    b  4.728632  4.739751  4.788882  4.754861  4.795203  5.200185   100
#    c  7.388025  7.413684  7.503427  7.458444  7.555520  8.160925   100
#    d  7.390876  7.412686  7.530044  7.454453  7.533568  8.535544   100
#    e 12.019154 12.205608 12.430870 12.316084 12.581081 13.917336   100
#    f 15.712882 15.735975 15.875353 15.770043 15.861275 18.906262   100
zx8754
  • 52,746
  • 12
  • 114
  • 209
3

Use fixed = TRUE to match the pattern as a string:

gsub("\"", "", data, fixed = TRUE)
neilfws
  • 32,751
  • 5
  • 50
  • 63
3

@milan was faster : )

An approach with stringr would be

library(stringr)
str_replace(data, '[\"]', '')
allanvc
  • 1,096
  • 10
  • 23
3

This works as well:

gsub("\"", "", data)
Lennyy
  • 5,932
  • 2
  • 10
  • 23
2

You could also remove the first character, skipping over the backslash headache:

gsub("^.","",data)
P1storius
  • 917
  • 5
  • 12
0

I use combination of gsub() and noquote()

for (i in data){
   print(gsub('"','',(noquote(i))))
}