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

时间:2017-07-24 09:24:42

标签: r conditional-statements delete-row

我有一个数据帧,它是通过附加类似的数据帧来实现的。 样本文件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

3 个答案:

答案 0 :(得分:2)

mf[c(1,which(!(mf$RunStatus %in% c(" TowStarted "," TowStopped "))),nrow(mf)),]

答案 1 :(得分:1)

您可以使用dplyr

library(dplyr)
library(magrittr)

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

我在其他数据上进行了测试,但它确实有用。

答案 2 :(得分:0)

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)

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