0

I am trying to randomly sample 10 individuals from a population and repeat 1000 times. Is this possible? Here is my code so far and I am not quite sure if I am on the right track. I keep receiving the error "number of items to replace is not a multiple of replacement length".

Here is my code:

B<-1000
for (i in 1:B){

  FR3_Acropora_Sample[i]<-(sample(FR3_Acropora$Ratio,size=10,replace=TRUE))

}

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Sarah
  • 43
  • 4

2 Answers2

2

Consider replicate (wrapper to sapply):

# MATRIX
sample_matrix <- replicate(B, sample(FR3_Acropora$Ratio, size=10, replace=TRUE))

# LIST
sample_list <- replicate(B, sample(FR3_Acropora$Ratio, size=10, replace=TRUE),
                         simplify = FALSE)
Parfait
  • 104,375
  • 17
  • 94
  • 125
1

I believe you can accomplish this as follows. I create a sample dataset of numbers 1 through 50 - you'll skip this step of course. I initialize a vector of lists with a length of 100. I loop from 1 to 100 and choose a random sample to assign to each empty space in my vector. I can then access any sample with sampleList[[x]] where x is any number 1 to 100.

x <- c(1:50)

sampleList <- vector(mode="list", length=100)

for (i in 1:100) {
sampleList[[i]] = sample(x, size = 10, replace = TRUE)
}

Using your variable names, this would look like:

B<-1000
FR3_Acropora_Sample <- vector(mode="list", length=1000)
for (i in 1:B){

  FR3_Acropora_Sample[[i]]=sample(FR3_Acropora$Ratio,size=10,replace=TRUE)

}
superblowncolon
  • 183
  • 1
  • 1
  • 15