[R] How to avoid ifelse statement converting factor to character

Stavros Macrakis macrakis at alum.mit.edu
Wed Jun 24 20:51:47 CEST 2009


On Wed, Jun 24, 2009 at 12:34 PM, Mark Na<mtb954 at gmail.com> wrote:
> The problem is that after running the ifelse statement, data$SOCIAL_STATUS
> is converted from a factor to a character.
> Is there some way I can avoid this conversion?

I'm afraid that ifelse has very bizarre semantics when the yes and no
arguments don't have the same, atomic vector, type.

The quick workaround for the bizarre semantics (though it can have a
significant efficiency cost) is this:

       unlist( ifelse ( condition, as.list( yes ), as.list( no ) ) )

(This isn't perfect, either, but...)

Take a look at the man page for details and the warning:

     The mode of the result may depend on the value of 'test', and the
     class attribute of the result is taken from 'test' and may be
     inappropriate for the values selected from 'yes' and 'no'.

Some consequences of the definition of ifelse are:

Even if the classes of the yes and no arguments are identical, the
result does not necessarily have that class:

    ifelse(TRUE,as.raw(4),as.raw(5)) => error

    ifelse(TRUE,factor('x'),factor('x')) => 1      (integer)

    dates <- as.POSIXct(c('1990-1-1','2000-1-1'))
    ifelse(c(TRUE,FALSE),dates,dates)  =>  631170000 946702800  (double)

    ifelse(c(TRUE,FALSE),factor(c('x','y')),factor(c('y','x'))) => 1 1

If they have different classes, things get stranger:

    ifelse(c(TRUE,FALSE),c("a","b"),factor(c("c","d")))  =>  "a" "2"

    ifelse(c(TRUE,FALSE),list(1,2),as.raw(4))
    [[1]]
    [1] 1

    [[2]]
    [1] 04

Result is order-dependent:

    ifelse(c(TRUE,FALSE),as.raw(4),list(1,2))
    Error in ans[test & !nas] <- rep(yes, length.out = length(ans))[test &  :
    incompatible types (from raw to logical) in subassignment type fix

Welcome to R!

         -s




More information about the R-help mailing list