[R] data.frame subset?

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Nov 18 11:01:32 CET 2003



On Tue, 18 Nov 2003, Jeff D. Hamann wrote:

> Can someone tell me why this is since I can't seem to find an explination in
> the docs or FAQ for this.
> 
> Since there are no "BM" in the mtrs data.frame, but only in the "parent"
> data.frame (trees), I'm assuming the subset data.frame is still associated
> with the original. Is that correct and how would I create a completly
> separate data.frame since I'll be using by, lapply and such on the subset
> data.frame using "sp" which currently causes by to choke...
> 
> > mtrs <- trees[trees$m == 1,]
> > mtrs
>     plot tree sp m dbh tht   bfvol   cfvol
> 1      1    1 DF 1  44 185 3825.79  622.97
> 2      1    2 DF 1  38 188 3166.69  504.12
> 26     3    1 DF 1  42 185 3568.80  578.09
> 51     5    4 DF 1  30 189 2071.36  348.28
> 67     7    1 DF 1  33 168 2036.30  342.90
> 70     7    4 DF 1  45 180 3665.82  621.90
> 71     7    5 DF 1  30 160 1530.88  274.69
> 81     9    1 DF 1  44 175 3398.52  576.79
> 82     9    2 DF 1  68 205 9516.82 1461.21
> 95    11    1 DF 1  30 170 1775.60  299.54
> 96    11    2 DF 1  44 197 4249.98  680.64
> 97    11    3 DF 1  44 187 3914.88  632.42
> 107   11   13 WH 1  44 175 3398.52  576.79
> 121   13    2 WF 1  48 205 5241.14  827.57
> 126   13    7 DF 1  40 182 3188.77  522.43
> 138   15    2 DF 1  69 182 7852.22 1285.42
> 143   15    7 DF 1  28 142 1146.77  207.67
> 159   17    9 DF 1  12  88   99.13   25.43
> 177   19    7 DF 1  37 162 2309.06  391.48
> > attributes( mtrs$sp )
> $class
> [1] "factor"
> 
> $levels
> [1] "BM" "DF" "WF" "WH"
> 


A factor remembers all its levels unless you tell to drop them as in:
 mtrs <- trees[trees$m == 1, , drop = TRUE]

Uwe Ligges




More information about the R-help mailing list