[R] sampling problem - new to R

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 6 16:34:41 CEST 2007


A variation of Brian's idea of using resample in ?sample would be:

   set.seed(1) # makes sample reproducible
   aggregate(DF[1], DF[2], resample, size = 1)

Using resample ensures that the solution works even if some
of the Plots only have one Tree.  Some of the solutions
that were presented in this thread do not work properly in that case
as sample acts differently according to whether its first argument's
length is 1 or > 1.

On 6/6/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Tue, 5 Jun 2007, baldeck wrote:
>
> > I have a data set of individual trees and the plots that they are in:
> >
> > Tree      Plot
> > 56749    1
> > 63494    1
> > 87375    1
> > 37494    2
> > 92753    3
> > 34847    3
> > 38747    4 etc...
>
> You haven't told us what form the 'data set' is, but I will presume a data
> frame called DF.
>
> The obvious first step is to split by Plot.  Using 'resample' from ?sample
>
> sapply(with(DF, split(Tree, Plot)), resample, size=1)
>
> give a vector of trees ('individuals'?) with names the plots sampled from.
> That seems to be what you want, but if not please come back to us with a
> more extensive example including the desired output.
>
>
> > So each plot is represented once for every individual that occurrs in it.
> > Plots get different numbers of rows because there can be a different number
> > of individuals in each plot.
> >
> > I want to make a data frame that consists of one individual from each plot.
> > I would like to randomly choose one individual from each plot that is
> > present in the data set.  I will have to do this to multiple data sets which
> > may contain different plots, and may contain up to 1200 plots, so I can't
> > choose the plots by hand.
> >
> > Please help me with this.  I'm an ecologist and I'm in Panama, with no one
> > around who is educated in R.  Whoever solves this problem for me will be
> > acknowledged in any resulting publications.
> >
> > Thanks!
> > -Claire
> >
>
> --
> 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
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list