[R] TRUE/FALSE as numeric values

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 23 18:57:58 CET 2007


It is inefficient to use which() rather than a logical index, since you 
allocate two numeric index vectors (one the length of the original 
vector) and use an interpreted function rather than optimized C code.
Also, in this usage which() handles NAs incorrectly.

I think the clearest answer is probably

    with(RSF_EU, AREA[AREA <= x])

On Fri, 23 Feb 2007, Henrique Dallazuanna wrote:

> You can also:
>
> test <- RSF_EU[which(RSF_EU$AREA<=x),]
>
> On 23/02/07, Thomas Preuth <preuth at slf.ch> wrote:
>>
>> Hello,
>>
>> I want to select in a column of a dataframe all numbers smaller than a
>> value x
>> but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer:
>>> test
>> [1]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
>> FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
>> [18]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
>> TRUE  TRUE FALSE  TRUE  TRUE  TRUE
>> [35] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
>> TRUE  TRUE FALSE FALSE  TRUE FALSE
>> [52]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
>>
>> How can i get the values smaller than x and not the TRUE/FALSE reply?
>>
>> Thanks in advance,
>> Thomas
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
>
>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list