[R] sampling problem - new to R

Sarah Goslee sarah.goslee at gmail.com
Wed Jun 6 15:29:00 CEST 2007


Claire,

Here's one way to do it:

# first, generate some sample data to try
> treedata <- cbind.data.frame(Tree=1:25, Plot=sample(1:5, 25, replace=TRUE))
> treedata <- treedata[order(treedata$Plot),]
> treedata
   Tree Plot
1     1    1
2     2    1
6     6    1
9     9    1
11   11    1
17   17    1
18   18    1
23   23    1
13   13    2
16   16    2
25   25    2
5     5    3
10   10    3
4     4    4
7     7    4
8     8    4
24   24    4
3     3    5
12   12    5
14   14    5
15   15    5
19   19    5
20   20    5
21   21    5
22   22    5

# then randomly choose one tree from each plot
# getting a different random set each time
> sapply(split(treedata$Tree, treedata$Plot), sample, 1)
 1  2  3  4  5
 2 25 10  4 21
> sapply(split(treedata$Tree, treedata$Plot), sample, 1)
 1  2  3  4  5
23 13  5  4 14


Hope that solves it for you,
Sarah

On 6/5/07, baldeck <cabaldeck at yahoo.com> 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...
>
> 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
> --


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list