[R] Problem recoding several variables into one

Christopher David Desjardins cddesjardins at gmail.com
Wed Feb 4 00:15:22 CET 2009

I am running the following code:

ret <- data.frame(V1=rep(NA,dim(hhm)[1]))

years <- c("grade04","grade05","grade06","grade07","grade08")

for(i in 1:(length(years)-1)){
   ret[,i] <- ifelse(hhm[,years[i+1]]==hhm[,years[i]],1,0)}

ret$ret.s <- ifelse(ret$V1==1 | ret$V2==1 | ret$V3==1 | ret 

However, when it encounters an 'NA' in these variables it assigns  
ret.s an 'NA'. What I want it to do is either assign a 0 or a 1. A 1  
should occur if an individual has the same value for grade04 &  
grade05, grade05 & grade06, and so on. If the individual does not have  
this I would like to assign a value of 0. However, when it encounters  
a NA it assigns an NA. How can I overcome this?


More information about the R-help mailing list