Reputation: 69
In R, I have table:
Field1
&&hello
&hi
Hello
Hi
How to remove everything that starts with "&"?
gsub("&", "", A$Field1)
will only remove "&" but not "&&hello" or "&hi" as I need it. I tried "&*" or "&+" but it’s still not working.
Upvotes: 0
Views: 522
Reputation: 172
I'm not 100% sure what you're asking, but if you want to remove the lines with an ampersand in them, you could use:
a <- c("&hi", "&&hello", "Hello", "Hi")
b <- a[-grep("&", a)]
> b
[1] "Hello" "Hi"
Cheers, Jim
Upvotes: 0
Reputation: 89057
Using gsub
, you need to match the whole string:
gsub("^&.*", "", A$Field1)
but I find a combination of ifelse
and grepl
a little more elegant:
ifelse(grepl("^&", A$Field1), "", A$Field1)
If the goal is to replace it in your dataframe, then you can reassign:
A$Field1 <- ifelse(grepl("^&", A$Field1), "", A$Field1)
Upvotes: 1