Skip to content

sampling from data.frame

4 messages · axionator, jim holtman, Charles C. Berry

#
Hi all,
I have a data frame with "clustered" rows as follows:
Cu1  x1 y1 z1 ...
Cu1  x2 y2 z2 ...
Cu1  x3 y3 z3 ... # end of first cluster Cu1
Cu2  x4 y4 z4 ...
Cu2  x5 y5 z5
Cu2  ...               # end of second cluster Cu2
Cu3 ...
...
"cluster"-size is 3 in the example above (rows making up a cluster are
always consecutive). Is there any faster way to sample n clusters
(with replacement) from this dataframe and build up a new data frame
out of these sampled clusters? I use the "sample" function and a
for-loop.

Thanks in advance
Armin
#
Not sure exactly what you mean by 'sample' since you did not provide
an example of the expected output, or input data that could be used.
Here is an example of taking one sample from each cluster:
+     df[sample(.indx, 1),]
+ })
id data
C1 C1    1
C2 C2    4
C3 C3    9
C4 C4   11
C5 C5   15
+     df[sample(.indx, 1),]
+ })
id data
C1 C1    2
C2 C2    6
C3 C3    9
C4 C4   11
C5 C5   15
+     df[sample(.indx, 1),]
+ })
id data
C1 C1    3
C2 C2    4
C3 C3    8
C4 C4   10
C5 C5   13

        
On Tue, Dec 2, 2008 at 6:27 PM, axionator <axionator at gmail.com> wrote:

  
    
#
On Wed, 3 Dec 2008, axionator wrote:

            
Something like this:

cl.samps <- sample( split( df, df$cluster ), n.samps, repl=TRUE )

do.call( rbind, cl.samps )

If you need to identify the samples from which the rows came (versus just 
the originating clusters):

cl.samps2 <- lapply( seq(along=cl.samps),
 	function(x) cbind( cl.samps[[ x ]], new.cluster = x ) )

do.call( rbind, cl.samps2 )

HTH,

Chuck
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
#
sorry for being a little bit imprecise, Chuck interpreted my question
as desired.

Armin