Skip to content
Prev 164858 / 398503 Next

refer to next line within a data-frame an select cases

grep for "pos" or "neg" reducing the data frame to
the rows prior to those matched. With this reduced
data frame pick out those row2 for which "a" is in row1:

# input
DF <- data.frame(row1 = c("a", "pos", "a", "neg", "a", "neg", "a", "pos"),
 row2 = c(12, NA, 3, NA, 5, NA, 11, NA), stringsAsFactors = FALSE)

# soln 1. get pos and neg
pos <- with(DF[grep("pos", DF$row1)-1,], row2[row1 == "a"])
neg <- with(DF[grep("neg", DF$row1)-1,], row2[row1 == "a"])

An alternative is available if we can assume that rows with
pos or neg in row1 always have NA in row2.   In that case
na.locf from zoo can fill in the NAs with the prior value:

# soln 2
library(zoo)
pos <- with(DF, na.locf(row2)[row1 == "pos"])
neg <- with(DF, na.locf(row2)[row1 == "neg"])

# or this which creates variables neg, pos and a
# so we ignore the a:

# soln 3 (also requires na.locf from zoo):
attach(split(na.locf(DF$row2), DF$row1))
On Mon, Dec 15, 2008 at 10:35 PM, J?rg Gro? <joerg at licht-malerei.de> wrote: