[R] Basic vector logic not working

Bert Gunter gunter.berton at gene.com
Sat Jul 9 22:49:52 CEST 2011


On Sat, Jul 9, 2011 at 12:08 PM, Pete Brecknock <Peter.Brecknock at bp.com> wrote:
>
> DimmestLemming wrote:
>>
>> I am interning in a computer science lab and I'm very new to R. The
>> language basics are clear, but this particular problem isn't:
>>
>> I have a very large dataframe called "data" which holds data from Halo
>> matches. I'm trying to analyze a certain window such that data$deaths>20
>> and data$deaths<=27.
>>
>> When I enter the line
>>
>> kills = data$kills[data$deaths>20]
>>
>> or any single condition, it selects the region I want. However, when I
>> input
>>
>> kills = data$kills[data$deaths>20 && data$deaths<=27]
>>
>> "kills" becomes the entire vector data$kills, without selecting any
>> specific region.
>>
>> This is a simple problem, but I can't find anything wrong. How could I fix
>> it?

Read the Help files more carefully? --  in particular noting the
difference between
& and &&:  The first is vectorized; the second is not (as Peter's
solution below shows).

-- Bert

>>
>
> Maybe something like ...
>
> # example data
> d = data.frame(age=1:20, names=letters[1:20])
>
> # 1. subset using [
> d[d$age>10 & d$age<16,]
>
> # 2. subset using subset function
> subset(d,d$age>10 & d$age<16)
>
> HTH
>
> Pete
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Basic-vector-logic-not-working-

-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list