我有数据帧,看起来像这样:中的R用火柴在多个行中删除数据帧的行
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])
}
}
我能够使用上面的代码打印出我想要删除的两行我想知道什么是最优雅的解决方案来删除这些行?
如果在这里有正确的方法或者是否有一个库可以更容易地完成这种类型的事情,那么这个方法也适用于嵌套方法吗?
问候 乔纳森
感谢伊莫是一个非常好的和优雅(非常R)解决方案。我已经试过了,它给出了所需的结果。非常感谢。 –