dimanche 10 juillet 2016

How to take a thousand random samples in R?

I've read How to create a loop for generate a list of random samples in R?

I've scoured the internet for the answer to this question, but I just get generic loop problems.

I want to generate 5000 random uniform samples using sample and store them in a vector. I'm out of ideas so I'm coming here for help, my code:

Tests = NULL

for(i in c(1:5000)) { 
   Tests[i]<- sample(x = c(0:9), size = 50128, replace = T) 
   Record<-table(Tests)
}

I get the error:

In Tests[i] <- sample(x = c(0:9), size = 50128, replace = T) :
number of items to replace is not a multiple of replacement length

At least 50 times (probably 5000 times). I've tried changing the size of the sample, the number of samples,but nothing seems to get rid of this error. I'm trying to store all the samples in a table titled Record.

edit: I know how to make loops using apply/sapply/lapply, but I don't think that those would be good options for generating a ton of random samples because I don't think you could store them anywhere.




Aucun commentaire:

Enregistrer un commentaire