I am trying to get R to resample my dataset of two columns of age and length data for fish. I got it to work, but it is not resampling every replicate. Instead, it resamples my data once and then repeated it 5 times. Here is my dataset of 9 fish samples with an age and length for each one: Age Length 2 200 5 450 6 600 7 702 8 798 5 453 4 399 1 120 2 202 Here is my code which resamples my data to produce up to 9 different samples and creates a new dataset of 12 samples: testdat<-growth[sample(9,12,replace=T),] Now I want R to repeat this procedure 5 times. Here is my code: testdat2 <- replicate(5, sample(testdat), simplify=F) testdat2 Here is my output showing that it did it once and then just repeated the values:> testdat2[[1]] Age Length 1 2 200 9 2 202 8 1 120 5 8 798 4 7 702 6 5 453 1.1 2 200 4.1 7 702 4.2 7 702 5.1 8 798 4.3 7 702 6.1 5 453 [[2]] Age Length 1 2 200 9 2 202 8 1 120 5 8 798 4 7 702 6 5 453 1.1 2 200 4.1 7 702 4.2 7 702 5.1 8 798 4.3 7 702 6.1 5 453 [[3]] Age Length 1 2 200 9 2 202 8 1 120 5 8 798 4 7 702 6 5 453 1.1 2 200 4.1 7 702 4.2 7 702 5.1 8 798 4.3 7 702 6.1 5 453 [[4]] Length Age 1 200 2 9 202 2 8 120 1 5 798 8 4 702 7 6 453 5 1.1 200 2 4.1 702 7 4.2 702 7 5.1 798 8 4.3 702 7 6.1 453 5 [[5]] Length Age 1 200 2 9 202 2 8 120 1 5 798 8 4 702 7 6 453 5 1.1 200 2 4.1 702 7 4.2 702 7 5.1 798 8 4.3 702 7 6.1 453 5 Any advice on how to get R to resample each time would be greatly appreciated? Mike
I think that you want: replicate(5, growth[sample(9,12,replace=T),], simplify = FALSE) On Wed, Sep 29, 2010 at 3:19 PM, Michael Larkin <mlarkin@rsmas.miami.edu>wrote:> I am trying to get R to resample my dataset of two columns of age and > length > data for fish. I got it to work, but it is not resampling every replicate. > Instead, it resamples my data once and then repeated it 5 times. > > Here is my dataset of 9 fish samples with an age and length for each one: > Age Length > 2 200 > 5 450 > 6 600 > 7 702 > 8 798 > 5 453 > 4 399 > 1 120 > 2 202 > > > Here is my code which resamples my data to produce up to 9 different > samples > and creates a new dataset of 12 samples: > > testdat<-growth[sample(9,12,replace=T),] > > > Now I want R to repeat this procedure 5 times. Here is my code: > > > testdat2 <- replicate(5, sample(testdat), simplify=F) > testdat2 > > Here is my output showing that it did it once and then just repeated the > values: > > > > testdat2 > [[1]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[2]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[3]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[4]] > Length Age > 1 200 2 > 9 202 2 > 8 120 1 > 5 798 8 > 4 702 7 > 6 453 5 > 1.1 200 2 > 4.1 702 7 > 4.2 702 7 > 5.1 798 8 > 4.3 702 7 > 6.1 453 5 > > [[5]] > Length Age > 1 200 2 > 9 202 2 > 8 120 1 > 5 798 8 > 4 702 7 > 6 453 5 > 1.1 200 2 > 4.1 702 7 > 4.2 702 7 > 5.1 798 8 > 4.3 702 7 > 6.1 453 5 > > > Any advice on how to get R to resample each time would be greatly > appreciated? > > Mike > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi: The deal with replicate() is that its second argument is a *function*; more specifically, a function *call*. That's why Henrique's solution worked and your attempt didn't. Inside replicate(), if the function has arguments, they need to be supplied. This works: testdat <- function(df, n) df[sample(nrow(df), n, replace = TRUE), ] # function definition replicate(3, testdat(growth, 12), simplify = FALSE) ^^^^^^^^^^^^^^^^^^^ function call The argument simplify = FALSE means that replicate will not try to simplify the output from the function. I called your growth data fd, so> replicate(3, testdat(fd, 12), simplify = FALSE)[[1]] Age Length 7 4 399 8 1 120 6 5 453 1 2 200 2 5 450 5 8 798 9 2 202 2.1 5 450 7.1 4 399 6.1 5 453 7.2 4 399 6.2 5 453 [[2]] Age Length 9 2 202 5 8 798 6 5 453 9.1 2 202 9.2 2 202 5.1 8 798 3 6 600 1 2 200 8 1 120 2 5 450 5.2 8 798 9.3 2 202 [[3]] Age Length 3 6 600 3.1 6 600 9 2 202 7 4 399 5 8 798 1 2 200 4 7 702 6 5 453 5.1 8 798 5.2 8 798 2 5 450 9.1 2 202 The function is defined a little more generally so that you can control the sample size. HTH, Dennis On Wed, Sep 29, 2010 at 11:19 AM, Michael Larkin <mlarkin@rsmas.miami.edu>wrote:> I am trying to get R to resample my dataset of two columns of age and > length > data for fish. I got it to work, but it is not resampling every replicate. > Instead, it resamples my data once and then repeated it 5 times. > > Here is my dataset of 9 fish samples with an age and length for each one: > Age Length > 2 200 > 5 450 > 6 600 > 7 702 > 8 798 > 5 453 > 4 399 > 1 120 > 2 202 > > > Here is my code which resamples my data to produce up to 9 different > samples > and creates a new dataset of 12 samples: > > testdat<-growth[sample(9,12,replace=T),] > > > Now I want R to repeat this procedure 5 times. Here is my code: > > > testdat2 <- replicate(5, sample(testdat), simplify=F) > testdat2 > > Here is my output showing that it did it once and then just repeated the > values: > > > > testdat2 > [[1]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[2]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[3]] > Age Length > 1 2 200 > 9 2 202 > 8 1 120 > 5 8 798 > 4 7 702 > 6 5 453 > 1.1 2 200 > 4.1 7 702 > 4.2 7 702 > 5.1 8 798 > 4.3 7 702 > 6.1 5 453 > > [[4]] > Length Age > 1 200 2 > 9 202 2 > 8 120 1 > 5 798 8 > 4 702 7 > 6 453 5 > 1.1 200 2 > 4.1 702 7 > 4.2 702 7 > 5.1 798 8 > 4.3 702 7 > 6.1 453 5 > > [[5]] > Length Age > 1 200 2 > 9 202 2 > 8 120 1 > 5 798 8 > 4 702 7 > 6 453 5 > 1.1 200 2 > 4.1 702 7 > 4.2 702 7 > 5.1 798 8 > 4.3 702 7 > 6.1 453 5 > > > Any advice on how to get R to resample each time would be greatly > appreciated? > > Mike > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]