[R] data frame subsets?

David Winsemius dwinsemius at comcast.net
Fri Nov 13 12:49:56 CET 2009


On Nov 12, 2009, at 10:57 PM, Douglas M. Hultstrand wrote:

> Hello,
>
> I am trying to create data frame subsets based on binned temperature  
> data.  I have code working to create the bins (d.1 and d.2), but it  
> takes two steps, I was wondering if I could merge into one step.   
> See Below
>
> d
>   n year mo da hr   t   td  tw rh   kPa
> 1   1 1945  3  1  0 1.1  0.0 0.6 92 101.7
> 2   2 1945  3  1  1 2.8 -1.1 1.1 76 101.8
> 3   3 1945  3  1  2 2.2 -1.7 0.6 75 101.9
> 4   4 1945  3  1  3 1.7 -1.1 0.6 82 102.0
> 5   5 1945  3  1  4 1.7 -2.8 0.0 72 102.1
> 6   6 1945  3  1  5 1.7 -1.7 0.0 78 102.2
> 7   7 1945  3  1  6 1.1 -2.8 0.0 75 102.2
> 8   8 1945  3  1  7 1.1 -1.7 0.0 82 102.4
> 9   9 1945  3  1  8 1.7 -1.1 0.6 82 102.5
> 10 10 1945  3  1  9 2.8 -3.3 0.6 64 102.6
>
> d.1 <- d[d$t >= 1.0,]
> d.2 <- d.1[d.1$t < 2.0,]
>
> How can I make  d.1 and d.2 into one step? I have tried several  
> different methods such as example below.
> d.1 <- d[d$t >= 1.0, && d$t < 2.0,]

Instead of using "&&" , use "&". It is the vectorized version of the  
AND operation. "&&" only operated on the first two elements and  
returned a scalar rather than the vector you needed.

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list