[R] Error: cannot take a sample larger than the population

Chuck Cleland ccleland at optonline.net
Sat Dec 30 11:45:07 CET 2006


Aldi Kraja wrote:
> Hi,
> In Splus7 this statement
> xlrmN1 <- sample(c(0,1,2),400 ,prob=c(0.02 ,0.93 ,0.05 ))
> worked fine, but in R the interpreter reports that the length of the 
> vector to chose c(0,1,2) is shorter than the size of many times I want 
> to be selected from the vector c(0,1,2).
> Any good reason?
> See below the error.
> 
>  > xlrmN1 <- sample(c(0,1,2),400 ,prob=c(0.02 ,0.93 ,0.05 ))
> Error in sample(length(x), size, replace, prob) :
>         cannot take a sample larger than the population
>  when 'replace = FALSE'
> Execution halted

  So why not use replace = TRUE ?

xlrmN1 <- sample(c(0,1,2),400 ,prob=c(0.02 ,0.93 ,0.05 ), replace=TRUE)

table(xlrmN1)
xlrmN1
  0   1   2
  5 373  22

prop.table(table(xlrmN1))
xlrmN1
     0      1      2
0.0125 0.9325 0.0550

> TIA,
> 
> Aldi
> 
> --
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list