[R] Generating variable from 2 others in dataframe
o.mannion at auckland.ac.nz
o.mannion at auckland.ac.nz
Mon Mar 1 23:03:02 CET 2010
Suppose I have the following dataframe called test:
test<-data.frame(year=rep(1990:2003,5),id=gl(5,length(1990:2003)),eif=as.vector(sapply(1:5,function(z){a<-rep(0,length(1990:2003));a[sample(1:length(1990: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
More information about the R-help
mailing list