I would like to delete the lines which contain the opening bracket "(" from my dataframe.
I tried the following:
df[!grepl("(", df$Name),]
But this does not track down the (
sign
I would like to delete the lines which contain the opening bracket "(" from my dataframe.
I tried the following:
df[!grepl("(", df$Name),]
But this does not track down the (
sign
You have to double-escape the (
with \\
.
x <- c("asdf", "asdf", "df", "(as")
x[!grepl("\\(", x)]
# [1] "asdf" "asdf" "df"
Just apply this to your df like df[!grepl("\\(", df$Name), ]
You could also think about removing all puctuation characters by using regex:
x[!grepl("[[:punct:]]", x)]
As pointed out by @CSquare in the comments, here is a great summary about special characters in R regex
Additional input from the comments:
@Sotos: Gaining performance with pattern='('
and fixed = TRUE
since the regex could be bypassed.
x[!grepl('(', x, fixed = TRUE)]