On Fri, Dec 18, 2009 at 11:49 AM, <tlum...@u.washington.edu> wrote:

> On Fri, 18 Dec 2009, Gabriel Becker wrote:
>
>  My understanding is that all the really fast vectorized operations are
>> implemented down in C code, not in R. Thus if you wanted to write a
>> vectorized switch, which I agree would be rather nice to have, you'd need
>> to
>> do it down there and then write a .Call wrapper for it in R.
>>
>> The ifelse (C) code would probably be a good place to start looking in
>> terms
>> of how to write it.
>>
>
> Gabe: ifelse() is not in C, nor is it really fast (though it is better than
> it used to be).
>

You are right of course.  Sorry for the noise.


>
> Stavros: I don't think there is a standard idiom.  I tend to use match() to
> work out which choice applies for each element, or nested ifelse if there
> are only three choices.
>
>      -thomas
>
>
>  Gabe
>>
>> On Fri, Dec 18, 2009 at 11:07 AM, Stavros Macrakis <macra...@alum.mit.edu
>> >wrote:
>>
>>  What is the 'idiomatic' way of writing a vectorized switch statement?
>>>
>>> That is, I would like to write, e.g.,
>>>
>>>        vswitch( c('a','x','b','a'),
>>>                     a= 1:4,
>>>                     b=11:14,
>>>                     100 )
>>>         => c(1, 100, 13, 4 )
>>>
>>> equivalent to
>>>
>>>       ifelse( c('a','x','b','a') == 'a', 1:4,
>>>                  ifelse( c('a','x','b','a') == 'b', 11:14,
>>>                             100 ) )
>>>
>>> A simple way of doing this is (leaving aside the default case):
>>>
>>>      colchoose <- function(frame,selector)
>>>          mapply(function(a,b)frame[a,b],seq_along(frame[1]),selector))
>>>
>>>     colchoose( data.frame(a=1:4,b=11:14), c('a','b','b','a'))
>>>          => c(1,11,11,1)
>>>
>>> But of course this is not very efficient compared to the way ifelse
>>> works.
>>>
>>> Is there a standard function or idiom for this (am I missing something
>>> obvious?), or should I write my own?
>>>
>>>            -s
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
> Thomas Lumley                   Assoc. Professor, Biostatistics
> tlum...@u.washington.edu        University of Washington, Seattle
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to