[R] selecting only specific rows in R

Charles C. Berry cberry at tajo.ucsd.edu
Sat Jun 5 19:10:27 CEST 2010


On Sat, 5 Jun 2010, stephen sefick wrote:

> Reproducible dummy example, as to the posting guide.  look at unique-
> you want to subset the data frame on all of the non-unique entries in
> the species column...
>
> x[x!=unique(x[,"species"]),]
>
> Something like that, maybe.  If I had some data then I could probably
> figure it out.  Use dput() or fake data to make a cut and paste-able
> example.
> HTH,
>
> Stephen

Or use one of the built in datasets - there are several that pertain to 
trees. Try

 	??tree

But yes, Adrienne, you should (as per Posting Guide) "provide commented, 
minimal, self-contained, reproducible code".

For example:

> ?Orange # find out about 'Orange' tree dataset
> ## rows with more than one tree of a given circumference:
> subset( Orange, xtabs(~circumference)[ as.character(circumference) ] > 1 
)
    Tree  age circumference
1     1  118            30
4     1 1004           115
6     1 1372           142
13    2 1372           203
14    2 1582           203
15    3  118            30
19    3 1231           115
29    5  118            30
33    5 1231           142
>

> # more detail to suggest why this works:
> with( Orange, xtabs(~circumference)[as.character(circumference)] > 1 )
[output deleted]

Chuck




>
> On Sat, Jun 5, 2010 at 10:00 AM, Adrienne Keller
> <adrienne.keller at umontana.edu> wrote:
>> Hi, I have a data frame with columns as follows: tree species (independent
>> variable) and several dependent variables (e.g. carbon, nitrogen,
>> phosphorus). Each row represents one tree sample. Some tree samples are
>> unique species in the data frame while other species were replicated (i.e.
>> rows 1,2,3 may be identical for the "tree species" column but have different
>> values for the other dependent variable columns). I want to create a new
>> data frame that selects only the tree species that have replicates. In other
>> words, I want to select all rows that have at least one replicate in the
>> column "tree species".
>>
>> Ideas on how to write such a function?
>>
>> Thanks,
>>
>> Adrienne Keller
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>
>
>
> -- 
> Stephen Sefick
> ____________________________________
> | Auburn University                                   |
> | Department of Biological Sciences           |
> | 331 Funchess Hall                                  |
> | Auburn, Alabama                                   |
> | 36849                                                    |
> |___________________________________|
> | sas0025 at auburn.edu                             |
> | http://www.auburn.edu/~sas0025             |
> |___________________________________|
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> 								-K. Mullis
>
> ______________________________________________
> R-help at r-project.org 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list