我想从我的数据框中删除包含左括号“(”的行。
我尝试了以下方法:
df[!grepl("(", df$Name),]
但这并没有追踪到 (
标志
最佳答案
您必须使用 \\
对 (
进行两次转义。
x <- c("asdf", "asdf", "df", "(as")
x[!grepl("\\(", x)]
# [1] "asdf" "asdf" "df"
只需将其应用到您的 df,例如 df[!grepl("\\(", df$Name), ]
您还可以考虑使用正则表达式删除所有标点符号:
x[!grepl("[[:punct:]]", x)]
正如@CSquare 在评论中指出的那样,here is a great summary about special characters in R regex
来自评论的额外输入:
@Sotos:通过 pattern='('
和 fixed = TRUE
获得性能,因为可以绕过正则表达式。
x[!grepl('(', x, fixed = TRUE)]
关于r - 如何删除带有特殊字符的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45634741/