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]]

