Hello If I have x=c(3,2,6,1) and n=length(x), are the following codes equivalent?? sample(x,1,replace=TRUE) and sample(x,1,replace=TRUE,prob=rep(1/n , n) ) Regards [[alternative HTML version deleted]]
Yes and no. Same effect, but you won't get the same random numbers because -- I believe -- a different algorithm is used. grep the source for sample and sample2 if you're interested. Cheers, Michael On Sat, Oct 6, 2012 at 5:02 PM, solafah bh <solafahbh at yahoo.com> wrote:> Hello > If I have x=c(3,2,6,1) and n=length(x), are the following codes equivalent?? > sample(x,1,replace=TRUE) and sample(x,1,replace=TRUE,prob=rep(1/n , n) ) > Regards > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org 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. >
Hi, They get different results: with the same set.seed() ?x=c(3,2,6,1) ?n=length(x) ?set.seed(1) ?sample(x,1,replace=TRUE)? #[1] 2 set.seed(1) ?sample(x,1,replace=TRUE,prob=rep(1/n , n) ) #[1] 6 ?identical(sample(x,1,replace=TRUE),sample(x,1,replace=TRUE,prob=rep(1/n , n) )) #[1] FALSE A.K. ----- Original Message ----- From: solafah bh <solafahbh at yahoo.com> To: R help mailing list <r-help at r-project.org> Cc: Sent: Saturday, October 6, 2012 12:02 PM Subject: [R] sample Hello If I have x=c(3,2,6,1) and n=length(x), are the following codes equivalent?? sample(x,1,replace=TRUE)??? and?????? sample(x,1,replace=TRUE,prob=rep(1/n , n) ) Regards ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.