You have to nest the ifelse's: test$hhtype <- with(test, ifelse(eif==1,1,ifelse(year<2000,2,3)))
Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of o.mann...@auckland.ac.nz Sent: Monday, March 01, 2010 2:03 PM To: 'r-help@r-project.org' Subject: [R] Generating variable from 2 others in dataframe Suppose I have the following dataframe called test: test<-data.frame(year=rep(1990:2003,5),id=gl(5,length(1990:2003)),eif=as.vec tor(sapply(1:5,function(z){a<-rep(0,length(1990:2003));a[sample(1:length(199 0:2003),sample(1:2,1))]<-1;a}))) year id eif 1990 1 0 1991 1 0 1992 1 0 2000 1 1 1994 1 0 1995 1 0 2001 1 0 1997 1 1 .... I want to create a new variable in this dataframe called "hhtype" according to the logic: If eif==1, hhtype = 1 Else if year < 2000, hhtype = 2 Else hhtype = 3 The result would be year id eif hhtype 1990 1 0 2 1991 1 0 2 1992 1 0 2 2000 1 1 1 1994 1 0 2 1995 1 0 2 2001 1 0 3 1997 1 1 1 I think I can do this with some combination of apply and ifelse, but so far have not succeeded in putting it all together. Any help would be very much appreciated! Thankyou, Oliver ______________________________________________ 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. ______________________________________________ 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.