Yeah, the reason I didn't use ifelse is because I've got multiple variables to manipulate based on the if statement, some factors and some numeric. I have to look at the factor variables, and based on that, either use one series of variables or another.
With the multiple if statements I need to check for, I though for statements with the if/else if conditional statement was better than nested ifelse functions. For example #example expanded on rm(list=ls()) v1.factor <- c("S","S","D","D","D",NA) v2.factor <- c("D","D","S","S","S","S") v3 <- c(1,0,0,0,0,NA) v4 <- c(0,0,1,1,0,0) test.data <- data.frame(v1.factor,v2.factor, v3, v4) for (i in 1:nrow(test.data) ) { if ( (is.na(v1.factor[i])==TRUE) & (is.na(v2.factor[i])==TRUE)) {test.data$newvar[i] <- NA; test.data$newvar2[i] <- NA}#IF 1 else if ( is.na(v1.factor[i])==TRUE & is.na(v2.factor[i])==FALSE) {test.data$newvar[i] <- test.data$v1.factor[i]; test.data$newvar2[i] <- test.data$v4[i]} #IF 2 else if ( is.na(v1.factor[i])==FALSE & is.na(v2.factor[i])==TRUE) {test.data$newvar[i] <- test.data$v2.factor[i]; test.data$newvar2[i] <- test.data$v3[i]} #IF 3 else if ( is.na(v1.factor[i])==FALSE & is.na(v2.factor[i])==FALSE) {test.data$newvar[i] <- test.data$v1.factor[i]; test.data$newvar2[i] <- test.data$v4[i]} #IF 4 } #End FOR #End example I'm not familiar with ifelse, but is there a way to use it in a nested format that would be better than my for loop structure? Or might I be better off finding a programming way of converting the new factor variables "back" to their factor values using the levels function? Sorry for the questions, I'm self taught at R and still trying to learn the best way to deal with these things. James On Thu, Jun 28, 2012 at 2:00 AM, Miguel Manese <jjon...@gmail.com> wrote: > Hi James, > > On Thu, Jun 28, 2012 at 12:33 AM, James Holland <holland.ag...@gmail.com> > wrote: > > I need to look through a dataset with two factor variables, and depending > > on certain criteria, create a new variable containing the data from one > of > > those other variables. > > > > The problem is, R keeps making my new variable an integer and saving the > > data as a 1 or 2 (I believe the levels of the factor). > > > > I've tried using as.factor in the IF output statement, but that doesn't > > seem to work. > > > > Any help is appreciated. > > > > > > > > #Sample code > > > > rm(list=ls()) > > > > > > v1.factor <- c("S","S","D","D","D",NA) > > v2.factor <- c("D","D","S","S","S","S") > > > > test.data <- data.frame(v1.factor,v2.factor) > > The vectorized way to do that would be > > # v1.factor if present, else v2.factor > test.data$newvar <- ifelse(!is.na(v1.factor), v1.factor, v2.factor) > > I suggest you work with the character levels first then convert it > into a factor, e.g. if v1.factor & v2.factor are already factors, do: > > test.data$newvar <- as.factor(ifelse(!is.na(v1.factor), > as.character(v1.factor), as.character(v2.factor))) > > > > Regards, > > Jon > [[alternative HTML version deleted]] ______________________________________________ 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.