Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R says "Cannot take a sample larger than the population" -- but I am not taking a sample larger than the population

Tags:

r

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'

like image 555
ahandler Avatar asked Oct 29 '13 01:10

ahandler


People also ask

What is the sample function in R?

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)


2 Answers

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),] 
like image 89
Aaron left Stack Overflow Avatar answered Sep 19 '22 12:09

Aaron left Stack Overflow


You can also use $ to select the specific column within your data set you want to sample from. Ex: m <- sample(dataframename$variable, 3500)

like image 33
LorettaA Avatar answered Sep 18 '22 12:09

LorettaA