This should do what you want -- not sure what happens after the 4th less than 5.
> a <- rep(c(3,5,7), 4)
> b <- rep(c(NA,1,2), 4)
> df <- data.frame(a,b)
> # determine which ones are < 5 and count the occurances
> less.5 <- cumsum(df$a < 5)
> # create new value
> df$c <- ifelse(df$a < 5, (less.5 +
Hello, R-helpers.
I have a data frame below called df. I want to add a variable c, based on
the following logic:
if df$a < 5 then the first two times this condition is met place a 1 in c,
the next two times this condition is met place a 2 in c and when the
condition is not met let c equal df$b.
2 matches
Mail list logo