如何在R中按名称模式删除行?

时间:2015-01-07 15:20:05

标签: regex r grep

我的样本数据集具有以下外观。

Country      Population        Capital             Area
A            210000210         Sydney/Landon       10000000
B            420000000         Landon              42100000
C            500000            Italy42/Rome1       9200000   
D            520000100         Dubai/Vienna21A     720000

如何删除列中带有模式/的整行。我试图查看以下链接R: Delete rows based on different values following a certain pattern,但它没有帮助。

2 个答案:

答案 0 :(得分:2)

您可以尝试grepl

df[!grepl('[/]', df$Capital),]
#   Country Population Capital     Area
#2       B  420000000  Landon 42100000

答案 1 :(得分:0)

library(stringr)
library(tidyverse)
df2 <- df %>% 
  filter(!str_detect(Capital, "\\/"))
#   Country Population Capital     Area
# 1       B  420000000  Landon 42100000

数据

df <- structure(list(Country = c("A", "B", "C", "D"), Population = c(210000210L,420000000L, 500000L, 520000100L), 
                     Capital = c("Sydney/Landon", "Landon", "Italy42/Rome1", "Dubai/Vienna21A"), 
                     Area = c(10000000L, 42100000L, 9200000L, 720000L)), class = "data.frame", row.names = c(NA,-4L))