如果该列具有" ClearPrevious",我想删除整行及其上面的行。但是我有多顺序" ClearPrevious"这让事情变得有点复杂。
输入:
tmp <- data.frame(V1 = c("event1", "ClearPrevious", "event2", "event3", "event4", "ClearPrevious", "ClearPrevious", "event5"))
我的输出:
1 event2
2 event3
3 event5
期望的输出:
1 event2
2 event5
我的尝试失败代码:
row_delete <- c(which(tmp$V1 == "ClearPrevious"))
row_to_be_deleted <- c(row_delete-1)
View(tmp[-c(row_to_be_deleted, row_delete),])
答案 0 :(得分:2)
这是我试过的:
rm(list=ls())
tmp <- data.frame(V1 = c("event1", "ClearPrevious", "event2", "event3",
"event4", "ClearPrevious", "ClearPrevious", "event5"),
stringsAsFactors = F)
while(any(grep('ClearPrevious', tmp$V1)))
{
clearindex <- which(tmp$V1 == 'ClearPrevious')
event_index <- grep('event', tmp$V1)
toremove <- intersect(clearindex-1, event_index)
V1 <- tmp[-c(toremove, toremove+1),]
tmp <- as.data.frame(V1)
}
tmp
# event2
# event5
答案 1 :(得分:2)
一种方法:
mutate(tmp,m=`length<-`(V1[-1],nrow(tmp)))%>%
with(V1[(!m=="ClearPrevious"|is.na(m))&!V1=="ClearPrevious"])
[1] event2 event3 event5
fun=function(s) {
while(any(grepl("ClearPrevious",s)))
s=trimws(gsub("\\w+\\s+ClearPrevious","",paste(s,collapse = " ")))
strsplit(s,"\\s+")[[1]]
}
fun(tmp$V1)
[1] "event2" "event5"