Hello R Help List, I am an R novice and trying to use the ifelse function to create a new binary variable based off of the responses of two other binary variables; NAs are involved. I pulled it off almost successfully, but when I checked the counts of my new variable for accuracy, I found that a small portion of the NA cases were not being passed through as NAs, but as "0" counts in my new variable. My many attempts at creating a nested ifelse statement that would pass the NAs through properly have not been successful. Any help is greatly appreciated.
Here is a MRE:? library(RCurl) data <- getURL("https://raw.githubusercontent.com/cbenjamin1821/careertech-ed/master/elsq2wbl.csv") elsq2wbl <- read.csv(text = data) ##Recoding Negative Responses to NA elsq2wbl [elsq2wbl[, "EVERRELJOB"] < -3, "EVERRELJOB"] <- NA elsq2wbl [elsq2wbl[, "PSWBL"] < -2, "PSWBL"] <- NA #Labeling categorical variable levels elsq2wbl$EVERRELJOB <- factor(elsq2wbl$EVERRELJOB, levels = c(0,1), labels = c("No","Yes")) elsq2wbl$PSWBL <- factor(elsq2wbl$PSWBL, levels = c(0,1), labels = c("No","Yes")) ##Trying to create a new variable to indicate if the student had a job #related to the college studies that was NOT a WBL experience elsq2wbl$NONWBLRELJOB <- ifelse(elsq2wbl$PSWBL=="No" & elsq2wbl$EVERRELJOB=="Yes",1,0) #Cross tab to check counts of two variables that new variable is based upon xtabs(~PSWBL+EVERRELJOB,subset(elsq2wbl,BYSCTRL==1&G10COHRT==1),addNA=TRUE) #Checking count of newly created variable Q2sub <- subset(elsq2wbl,BYSCTRL==1&G10COHRT==1) library(plyr) count(Q2sub,'NONWBLRELJOB') #The new variable has the correct count of "1", but 88 cases too many for "0" #The cross tab shows 20 and 68 NA cases that are being incorrectly counted as "0" in the new variable #My other approach at trying to handle the NAs properly-returns an error elsq2wbl$NONWBLRELJOB <- ifelse(elsq2wbl$PSWBL=="No" & elsq2wbl$EVERRELJOB=="Yes",1,ifelse(is.na(elsq2wbl$PSWBL)&is.na(elsq2wbl$EVERRELJOB),NA, ifelse(elsq2wbl$PSWBL!="No" & elsq2wbl$EVERRELJOB!="Yes",0))) Courtney Benjamin Broome-Tioga BOCES Automotive Technology II Teacher Located at Gault Toyota Doctoral Candidate-Educational Theory & Practice State University of New York at Binghamton cbenj...@btboces.org<mailto:cbenj...@btboces.org> 607-763-8633 [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.