I have a dataframe which made by appending similar dataframes. sample file 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
I need to keep first and last rows. but need to delete rows with RunStatus=="TowStarted" and RunStatus=="TowStopped".
mf<- read.csv("C:/Video_sledge/Output/merge.csv")
mf1 <-mf[ !grepl("TowStarted", mf$RunStatus) , ]
mf2 <-mf1[ !grepl("TowStopped", mf1$RunStatus) , ]
but this code deletes first and last rows.
How delete middle rows with test condition (RunStatus=="TowStarted" and RunStatus=="TowStopped") while keeping first and last rows?
expected output as follows
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
mf[c(1,which(!(mf$RunStatus %in% c(" TowStarted "," TowStopped "))),nrow(mf)),]
You could use the dplyr
package
library(dplyr)
library(magrittr)
df %>%
filter(!(RunStatus %in% c("TowStarted", "TowStopped")) | row_number() %in% c(1, nrow(df)))
I tested it on other data and it worked.
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)
this code gives me above output. but I think someone has a better solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.