[R] If statement - copying a factor variable to a new variable

Miguel Manese jjonphl at gmail.com
Thu Jun 28 09:00:42 CEST 2012


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



More information about the R-help mailing list