通过保留数据框的第一行和最后一行来删除具有特定值的行

Delete rows with specific values by keeping first and last row of dataframe

我有一个数据框,它是通过附加类似的数据框制作的。 示例文件 https://drive.google.com/open?id=0BwswfhTezOETWmpPakpGOUl0V0E

Lat Lon Species BottomDepth RunStatus
6023.9796   518.5393        NA   TowStarted 
6023.9796   518.5393     Cucumber   25    
6023.9796   518.5393     Cucumber   25     
6023.9796   518.5392     Chank  25     
6023.9797   518.5392        NA   TowStarted 
6023.9797   518.5392        NA   TowStopped 
6023.9797   518.5392     Cucumber   29     
6023.9797   518.5392     Chank  29     
6023.9797   518.5392     Chank  29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391        NA   TowStarted 
6023.9797   518.5391        NA   TowStopped 
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Cucumber   29     
6023.9797   518.5391        NA   TowStarted 
6023.9797   518.5391        NA   TowStopped 
6023.9797   518.5391     Cucumber   29     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Chank  35     
6023.9797   518.5391        NA   TowStopped 

我需要保留第一行和最后一行。但需要删除 RunStatus=="TowStarted" 和 RunStatus=="TowStopped".

的行
mf<- read.csv("C:/Video_sledge/Output/merge.csv")
mf1 <-mf[ !grepl("TowStarted", mf$RunStatus) , ]
mf2 <-mf1[ !grepl("TowStopped", mf1$RunStatus) , ]

但是这段代码删除了第一行和最后一行。

如何删除具有测试条件(RunStatus=="TowStarted" 和 RunStatus=="TowStopped")的中间行,同时保留第一行和最后一行?

预期输出如下

Lat Lon Species BottomDepth RunStatus
6023.9796   518.5393        NA   TowStarted 
6023.9796   518.5393     Cucumber   25    
6023.9796   518.5393     Cucumber   25     
6023.9796   518.5392     Chank  25     
6023.9797   518.5392     Cucumber   29     
6023.9797   518.5392     Chank  29     
6023.9797   518.5392     Chank  29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Sea Urchine    29     
6023.9797   518.5391     Cucumber   29     
6023.9797   518.5391     Cucumber   29     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Cucumber   35     
6023.9797   518.5391     Chank  35     
6023.9797   518.5391        NA   TowStopped

您可以使用 dplyr

library(dplyr)
library(magrittr)

df %>% 
  filter(!(RunStatus %in% c("TowStarted", "TowStopped")) | row_number() %in% c(1, nrow(df)))

我在其他数据上测试过它并且有效。

mf[c(1,which(!(mf$RunStatus %in% c(" TowStarted "," TowStopped "))),nrow(mf)),]
mf<- read.csv("C:/Video_sledge/Output/merge.csv")
frow <- mf[1,]
mf1 <-mf[ !grepl("TowStarted", mf$RunStatus) , ]
mf2 <-mf1[ !grepl("TowStopped", mf1$RunStatus) , ]
lrow <- mf[nrow(mf),]
f <-rbind(frow,mf2,lrow)

这段代码给出了上面的输出。但我认为有人有更好的解决方案。