过滤/删除不正确的行(上方和下方) [英] Filter / Delete incorrect rows (plus above and below)
本文介绍了过滤/删除不正确的行(上方和下方)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个很大的数据集。
我想删除所有不正确的行。另外,我想删除不正确的行之前和之后的一些行。
I have got a large dataset. I want to delete all incorrect rows. Additionally i want to delete some rows before and after the incorrect row.
我发现有帮助。但不幸的是,这对我来说不起作用。
我想我与行名有关,但我无法解决。
因此,我感谢任何有用的建议。
I found something helpful. But unfortunately it does not work out for me. I think i has to do with the "rownames" but i am not able to fix it. So i appreciate any helpful advices.
library(dplyr)
var1 <- 1:20
var2 <- c(1,1,1,1,0,1,0,1,1,1,0,0,1,1,1,1,1,0,1,1)
var3 <- rep(3, 20)
dat <- data.frame (var1, var2, var3)
View(dat)
extract.with.context <- function(x, rows, after = 0, before = 0) {
match.idx <- which(rownames(x) %in% rows)
span <- seq(from = -before, to = after)
extend.idx <- c(outer(match.idx, span, `+`))
extend.idx <- Filter(function(i) i > 0 & i <= nrow(x), extend.idx)
extend.idx <- sort(unique(extend.idx))
return(x[extend.idx, , drop = FALSE])
}
extract.with.context(dat, dat$var2==0, after = 1, before = 1)
View(dat)
#expected output
var1 <- c(1,2,3,9,14,15,16,20)
var2 <- c(1,1,1,1,1,1,1,1)
var3 <- rep(3,length(var2))
dat <- data.frame(var1, var2, var3)
View(dat)
推荐答案
我们得到行索引,其中列
等于值
,并删除索引 indx
的行, indx +之后
和 indx-之前
。
We get the index of rows where column
is equal to value
and remove the rows for indexes indx
, indx + after
and indx - before
.
extract.with.context <- function(df, column, value, before, after) {
indx <- which(df[[column]] == value)
df[-c(indx, indx + after, indx - before), ]
}
extract.with.context(dat, "var2", 0, 1, 1)
# var1 var2 var3
#1 1 1 3
#2 2 1 3
#3 3 1 3
#9 9 1 3
#14 14 1 3
#15 15 1 3
#16 16 1 3
#20 20 1 3
这篇关于过滤/删除不正确的行(上方和下方)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文