Here is my data:
x <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'
How to remove: "bbb","dddd" and "dddd"
So I can get:
"aaaaa","ccccc","abcd"
Thanks!
Here is my data:
x <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'
How to remove: "bbb","dddd" and "dddd"
So I can get:
"aaaaa","ccccc","abcd"
Thanks!
We could use gsub to match the pattern of double quote (") followed by one or more characters that are not a double quote ([^"]+) followed by a double quote (") and a , if present and replace with blank ("")
gsub('"[^"]+",?', "", txt)
[1] "aaaaa,ccccc,abcd"
If we need to keep the , in between
gsub('"[^"]+"', "", txt)
[1] "aaaaa,,ccccc,,abcd"
txt <- 'aaaaa,"bbb",ccccc,"dddd",abcd"dddd"'
You can drop the words with double quotes in them.
gsub('"\\w+",?', '', x)
#[1] "aaaaa,ccccc,abcd"
If you want them as vector of words -
strsplit(gsub('"\\w+",?', '', x), ',')[[1]]
#[1] "aaaaa" "ccccc" "abcd"