antecessor
antecessor

Reputation: 2800

Remove double quote \" symbol from string

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?

Upvotes: 11

Views: 14109

Answers (8)

Purnomo Setyawendha
Purnomo Setyawendha

Reputation: 98

I use combination of gsub() and noquote()

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

Upvotes: 0

zx8754
zx8754

Reputation: 56189

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

Upvotes: 7

milan
milan

Reputation: 4970

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)

Upvotes: 17

allanvc
allanvc

Reputation: 1156

@milan was faster : )

An approach with stringr would be

library(stringr)
str_replace(data, '[\"]', '')

Upvotes: 3

P1storius
P1storius

Reputation: 947

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

gsub("^.","",data)

Upvotes: 2

Lennyy
Lennyy

Reputation: 6132

This works as well:

gsub("\"", "", data)

Upvotes: 3

akrun
akrun

Reputation: 887301

Or we can just use '"' on the pattern

gsub('"', "", data)

Upvotes: 13

neilfws
neilfws

Reputation: 33782

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

gsub("\"", "", data, fixed = TRUE)

Upvotes: 3

Related Questions