How to delete rows where values are in parentheses?

只愿长相守 提交于 2021-02-05 11:44:16

问题


I am working with the following data frame:

Name       Height
Eric        64
(Joe)       67
Mike        66
Nick        72
(Dave)      69
Steve       73

I would like to delete all rows when the 'name' column starts with an open parenthesis "(". So the final data frame would look like:

Name       Height
Eric        64
Mike        66
Nick        72
Steve       73

回答1:


In the question the names to be excluded always start with a left parnethesis so if that is the general case use subset and startsWith like this:

subset(DF, !startsWith(Name, "("))
##    Name Height
## 1  Eric     64
## 3  Mike     66
## 4  Nick     72
## 6 Steve     73

Under the same assumption this would also work:

subset(DF, substr(Name, 1, 1) != "(")



回答2:


In base R you can use subset with grepl :

subset(df, !grepl('(', Name, fixed = TRUE))

#   Name Height
#1  Eric     64
#2  Mike     66
#3  Nick     72
#4 Steve     73

Or using dplyr and stringr :

library(dplyr)
library(stringr)

df %>% filter(str_detect(Name, fixed('('), negate = TRUE))

data

df <- structure(list(Name = c("Eric", "(Joe)", "Mike", "Nick", "(Dave)", 
"Steve"), Height = c(64L, 67L, 66L, 72L, 69L, 73L)), 
 class = "data.frame", row.names = c(NA, -6L))



回答3:


Succinctly you may subset using brackets [ ] by identifying the parenthesis rows using grep, which gives the row numbers and the preceding - negates them. Because "(" is a special character, we need to escape it using backslashes.

d[-grep("\\(", d$Name), ]
#    Name Height
# 1  Eric     64
# 3  Mike     66
# 4  Nick     72
# 6 Steve     73


来源:https://stackoverflow.com/questions/65501661/how-to-delete-rows-where-values-are-in-parentheses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!