Reputation: 63
I have character strings such as "76.1" and "76.1_0". I'd like to remove all elements of my dataframe that contain either a "." or a "_".
I've tried
df <- df[!grepl("_", "."),]
which didn't work. I know it's my regex that's the issue but I'm not sure how to proceed.
Any help would be appreciated, thank you!
Upvotes: 2
Views: 284
Reputation: 48241
Given an example vector
(x <- c("76.1", "76.1_0", "1", "a"))
# [1] "76.1" "76.1_0" "1" "a"
one gets the desired result with, e.g.,
(x <- x[!grepl("\\.|_", x)])
# [1] "1" "a"
where I used grepl
differently in a couple of aspects
1) The first argument, pattern
, has to be a character string ("\\.|_"
) rather than multiple arguments, as you tried ("_", "."
). See ?grepl
.
2) As to "combine" two symbols, one needs to use |
, which stands for OR.
3) .
is a special character and needs to be escaped with \\
.
4) You didn't provide the second argument, x
; i.e., where the changes should be done.
Upvotes: 2