【发布时间】:2016-09-15 12:06:44
【问题描述】:
我的数据框如下所示:
content ChatPosition
This is a start line START
This is a middle line MIDDLE
This is a middle line MIDDLE
This is the last line END
This is a start line with a subsequent middle or end START
This is another start line without a middle or an end START
This is a start line START
This is a middle line MIDDLE
This is the last line END
content <- c("This is a start line" , "This is a middle line" , "This is a middle line" ,"This is the last line" ,
"This is a start line with a subsequent middle or end" , "This is another start line without a middle or an end" ,
"This is a start line" , "This is a middle line" , "This is the last line")
ChatPosition <- c("START" , "MIDDLE" , "MIDDLE" , "END" , "START" ,"START" , "START" ,"MIDDLE" , "END")
df <- data.frame(content, ChatPosition)
我想删除包含开头的行,但前提是下一行在 ChatPosition 列中不包含 MIDDLE 或 END。
content ChatPosition
This is a start line START
This is a middle line MIDDLE
This is a middle line MIDDLE
This is the last line END
This is a start line START
This is a middle line MIDDLE
This is the last line END
nrow(df)
jjj <- 0
for(jjj in 1:nrow(df))
{
# Check of a match of two STARTS over over multiple lines.
if (df$ChatPosition[jjj]=="START" && df$ChatPosition[jjj+1]=="START")
{
print(df$content[jjj])
}
}
我能够使用上面的代码打印出我想删除的两行我想知道删除这些行的最优雅的解决方案是什么?
如果这里的方法正确,还是有嵌套的 for 或者是否有一个库可以更轻松地完成这种类型的事情?
问候 乔纳森
【问题讨论】: