Exclude all records below specific row in R -
the data have contain 3 variable 3 unique ids , each has multiple records. see below
id <- c(rep(1,7), rep(2,6), rep(3,5), rep(4,6)) t <- c(seq(1,7), seq(1,6), seq(1,5), rep(2,6)) y <- c(rep(6,7), rep(1,6), rep(6,5), rep(0.2,6)) z <- c(5,0,0,0,1,0,0,0,0,-1,0,0,0,4,2,nan,0,1,0,0,1,inf,inf, inf) dat1 <- data.frame(id, t, y, z)
for each id, if value of z below 0 (negative), nan, inf, or -inf, need exclude record , records below it.
for data, new processed data like:
id <- c(rep(1,7), rep(2,2), rep(3,2), rep(4,3)) t <- c(seq(1,7), seq(1,2), seq(1,2), rep(2,3)) y <- c(rep(6,7), rep(1,2), rep(6,2), rep(0.2,3)) z <- c(5,0,0,0,1,0,0,0,0,4,2,0,0,1) dat2 <- data.frame(id, t, y, z)
ave
, group-by substitute. using cumsum
counter identify subsequent rows deletion:
dat1[with(dat1, ave(z < 0 | (!is.finite(z)), id, fun=cumsum) == 0),]
quick check see match, exception of rownames not lining up:
all.equal( dat2, dat1[with(dat1, ave(z < 0 | (!is.finite(z)), id, fun=cumsum) == 0),], check.attributes=false ) #[1] true
Comments
Post a Comment