[R] How to avoid ifelse statement converting factor to character
Rolf Turner
r.turner at auckland.ac.nz
Thu Jun 25 03:04:43 CEST 2009
On 25/06/2009, at 12:27 PM, Craig P. Pyrame wrote:
>
> Dear Stavros,
>
> What you discuss below is somewhat scary to me as an R newbie. Is
> this
> just an incident, a bug perhaps, or rather the way things typically go
> in R, as your "Welcome to R!" seems to suggest? I have just
> started to
> learn R, and my initial euphoria of the "I can do anything with it!"
> sort is gradually turning into an "I can't get why it doesn't work"
> and
> "I can't get how to make this work" depression. I would be happy to
> blame this on my incompetence and incapability, but would also like to
> hear if it is not R itself that causes me to fail.
Do not get your knickers in a twist. R works simply and
straightforwardly
in simple straightforward situations. In less simple and less
straightforward
situations life gets more complicated. Don't dive into such situations
without making sure you understand them. Check your results to make
sure
you have not overlooked a subtlety.
In respect of the current discussion of ifelse() --- the original
problem arose
because the values of ``yes'' and ``no'' were of different modes.
It is obvious
that in such instances a decision will have to be made about the
mode of the
result. The appropriateness of the designers' decision may be
disputed, but
you have to admit that some decision had to be made. Recognize that
and all
the mystery goes away.
If you don't understand what's going on, then just stick to using
ifelse() only
when ``yes'' and ``no'' have the same mode.
Using things like as.raw() or taking one of ``yes'' and ``no'' to be
a list is getting
into territory where you need to be quite sophisticated and quite
careful. Unless
you are both, don't go there. I consider myself to be both (in
respect of R at
least) and I *still* would be very reluctant to go there.
Bottom line: R is easy to use at any level, but in order to use it
a ``high'' level
you need to understand the high level. Don't attempt to run before
you can crawl.
cheers,
Rolf Turner
> Best regards,
> Craig
>
>
> Stavros Macrakis wrote:
>> 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!
>>
>
> ______________________________________________
> R-help at 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.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list