Hi, I am trying to assign values to records based conditionally on other records within a dataframe. For example, in the following dataframe the "NA" value in dat$age would be replaced if the age status for that individual and specific year can be found in another record. Ideally this would then also assign the same age status if the individual is recorded in a later year.
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<-cbind(id,year,age) dat<-data.frame(dat) I am attempting to transform the age variable to; "Adult","Adult","Adult","Adult", "Adult","Adult","Adult","Adult","Adult","NA","Adult","Adult","Adult","Adult" I have used the following code to cross-reference the age data across records of the same year but am having trouble getting this work. I am also unsure how to cross-reference this to assign age status to records of the same individual recorded in later years. id<-unique(dat$id) year<-c(1,2,3,8) namer<-cbind(pit,season) namer<-data.frame(namer) for (i in 1:nrow(namer)) { dat$age[dat$id == namer[i,1] & dat$year== namer[i,2]] <- "Adult" } Any help would be gratefully recieved, Thanks -- View this message in context: http://r.789695.n4.nabble.com/conditional-value-assignment-tp4646945.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.