[R] How to avoid ifelse statement converting factor to character
Craig P. Pyrame
crappyr at gmail.com
Thu Jun 25 18:47:18 CEST 2009
Dear Rolf,
Rolf Turner wrote:
>
> 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.
>
Yes, sure, but this sounds to me almost like what some others seem to
have already suggested on this list - use R for tasks where you can
simply apply any of the numerous excellent package functions (for
example, to fit a model), but avoid using it for programming, because it
inevitably leads to problems - is this what you are saying?
> 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.
>
I wasn't really updated on the original example, but yes, creating a
programming language involves numerous difficult decisions that have to
be made, often in a relatively ad hoc fashion. But once you point me to
the original example, I have looked into what Stavros wrote, and the
following strikes me as one of those cases where I would feel that
either me or R is wrong:
> ifelse(TRUE,factor('x'),factor('x')) => 1 (integer)
The man page Stavros quotes states that the class attribute of the
result is taken from 'test', which clearly is not the case:
> class(TRUE) => logical
> class(ifelse(TRUE,factor('x'),factor('x'))) => integer
And also, I find myself incapable of making sense of the "may" in "the
mode of the result may depend on the value of 'test'" - may in what
sense? Will in 2.9 but not in 2.8? Will if test is logical but not if
it is raw? I am not particularly into programming languages, and
especially their semantics, so it may be my fault that I don't know what
such "may" may mean, but I am not quite sure. Could you help me?
> If you don't understand what's going on, then just stick to using
> ifelse() only
> when ``yes'' and ``no'' have the same mode.
Yes, in my poor R programming I make efforts to avoid involving myself
in tricky situations, but it is not always helpful enough, I assure you.
>
> 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.
>
I am not sure, is this supposed to be an argument in favor of using R?
> 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.
I am a bit reluctant to tell you this, Rolf, as there is likely much my
own fault involved, but I attempt to crawl, not to run, and this is
where I get into pains with R. Examples such as those of Stavros have
no place in my code, and yet I find it surprisingly difficult to
understand why large parts of my code don't work as expected - having
read the man pages carefully enough, I'd think.
Best regards,
Craig
More information about the R-help
mailing list