仅删除r中以给定字符串开头的行

时间:2015-08-05 19:39:05

标签: regex r subset

我需要删除以列中给定字符串开头的所有行。在此示例中,删除以example2开头的所有行。在着陆栏

我有一个数据集EX:

my.data

BABY      MOM      LANDING
mark      dina    example1.bo
tom       becky   example2.do
brad      tina    example1.eo
mark      dina    example1.ro
bob       kindra  example2.ao
brad      tina    example1.c3po

我希望我的新数据是:

new.data

BABY      MOM      LANDING
mark      dina    example1.bo
brad      tina    example1.eo
mark      dina    example1.ro
brad      tina    example1.c3po

如何告诉R删除my.data $ LANDING以' example2开头的所有行。'

这是我的代码

indx1 <- grepl('^community.', ga.data$pagePath)

ga.data[!indx1, ]

但是我仍然在pagePath列中有community.xxxx.com/

的项目

不确定我做错了什么

2 个答案:

答案 0 :(得分:3)

只需使用grepl创建逻辑索引,然后使用!运算符创建子集。

 ind <- grepl("^example2", df$LANDING)

 df[!ind, ]
  BABY  MOM       LANDING
1 mark dina   example1.bo
3 brad tina   example1.eo
4 mark dina   example1.ro
6 brad tina example1.c3po

答案 1 :(得分:2)

我们可以使用grep来获取以&#39; example2&#39;开头的元素的数字索引。来自#LANDING&#39;列并从&#39; my.data&#39;

中删除这些行
my.data[-grep('^example2', my.data$LANDING),]
# BABY  MOM       LANDING
#1 mark dina   example1.bo
#3 brad tina   example1.eo
#4 mark dina   example1.ro
#6 brad tina example1.c3po

数据

my.data <- structure(list(BABY = c("mark", "tom", "brad", "mark", "bob", 
"brad"), MOM = c("dina", "becky", "tina", "dina", "kindra", "tina"
), LANDING = c("example1.bo", "example2.do", "example1.eo",
"example1.ro", 
"example2.ao", "example1.c3po")), .Names = c("BABY", "MOM", "LANDING"
), class = "data.frame", row.names = c(NA, -6L))