Skip to content
Prev 308596 / 398506 Next

conditional value assignment

Hello,

First of all, two notes on the way you create your data.frame

1. Use age <- c("Adult", NA, ...etc...) _without_ quotes around NA. If 
you use quotes it will seen as a character, not as the value NA.

2. Do not cbind and then coerce to data.frame, use data.frame only.

So the data and an answer to your question could be

id <- c(1,1,1,1,2,2,2,2,2, 3,3,3, 4,4)
year <- c(1,1,1,2, 2,2,3,2,2, 2,3,4, 8,8)
age <- c("Adult",NA,NA,NA, "Adult",NA,NA,NA, "Adult",
         NA,"Adult",NA, NA,"Adult")
dat <- data.frame(id, year, age)

fun <- function(x){
     for(i in which(!is.na(x$age)))
         x$age[x$year >= x$year[i]] <- x$age[i]
     x
}

do.call(rbind, lapply(split(dat, dat$id), fun))


Also, I coudn't understand that 'namer' stuff. But you repeat the 
mistake cbind/data.frame.

Hope this helps,

Rui Barradas
Em 21-10-2012 23:53, penguins escreveu: