James Holland
2012-Jun-27 16:33 UTC
[R] If statement - copying a factor variable to a new variable
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) 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} else if ( is.na(v1.factor[i])==TRUE & is.na(v2.factor[i])==FALSE) {test.data$newvar[i] <- test.data$v1.factor[i]} else if ( is.na(v1.factor[i])==FALSE & is.na(v2.factor[i])==TRUE) {test.data$newvar[i] <- test.data$v2.factor[i]} else if ( is.na(v1.factor[i])==FALSE & is.na(v2.factor[i])==FALSE) {test.data$newvar[i] <- test.data$v1.factor[i]} } #End FOR #Also, I just wrote this up quickly as sample code, but I'm not sure why my 6th case is coming up as NA when it should be going to the second IF statement. #End sample code Thank you, James [[alternative HTML version deleted]]
Miguel Manese
2012-Jun-28 07:00 UTC
[R] If statement - copying a factor variable to a new variable
Hi James, On Thu, Jun 28, 2012 at 12:33 AM, James Holland <holland.aggie at 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