I am trying to pick 3500 random observations from a set of 5655 observations. But when I do so, R is throwing a strange error, saying that "cannot take a sample larger than the population when 'replace = FALSE'"
I am trying to take a sample smaller than the population. Why is R throwing this error?
nrow(males) [1] 5655 m = sample(males, 3500, replace = FALSE, prob = NULL)
Error in sample.int(length(x), size, replace, prob) : cannot take a sample larger than the population when 'replace = FALSE'
The sample() function in R allows you to take a random sample of elements from a dataset or a vector, either with or without replacement. The basic syntax for the sample() function is as follows: sample(x, size, replace = FALSE, prob = NULL)
You need to sample from the numbers, not from the data frame. Then use the results to get the sampled rows.
m <- males[sample(nrow(males), 3500, replace = FALSE, prob = NULL),]
You can also use $ to select the specific column within your data set you want to sample from. Ex: m <- sample(dataframename$variable, 3500)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With