Hi All, I'd like to randomly sample a vector N times, where each successive random sample increases in size. I have realised that the function sample does not take vectors for arguments. For example, x<-rnorm(20,0,1) sample(x,c(1,2,3)) ## will only return one random sample of size 1. The trick seems to be getting past the size argument of the function. I've tried different things without success. Any help would be most appreciated! Thanks, A -- View this message in context: http://r.789695.n4.nabble.com/Size-argument-in-sample-function-tp3605486p3605486.html Sent from the R help mailing list archive at Nabble.com.
I am sure there is a more elegant version of doing this. But this works: x<-rnorm(20) y<-matrix(1:5) #number of points to sample f<-function(z){sample(x,z)} apply(y,1,f) Just adjust y to your liking. HTH, Daniel alfredo wrote:> > Hi All, > > I'd like to randomly sample a vector N times, where each successive random > sample increases in size. I have realised that the function sample does > not take vectors for arguments. For example, > > x<-rnorm(20,0,1) > sample(x,c(1,2,3)) ## will only return one random sample of size 1. > > The trick seems to be getting past the size argument of the function. I've > tried different things without success. Any help would be most > appreciated! > > Thanks, > > A >-- View this message in context: http://r.789695.n4.nabble.com/Size-argument-in-sample-function-tp3605486p3606011.html Sent from the R help mailing list archive at Nabble.com.
you can do something like this x<-rnorm(20,0,1) time=c(1,2,3) sapply(1:length(time),function(t) sample(x,time[t])) Weidong Gu On Fri, Jun 17, 2011 at 9:59 AM, alfredo <alfredotello at gmail.com> wrote:> Hi All, > > I'd like to randomly sample a vector N times, where each successive random > sample increases in size. I have realised that the function sample does not > take vectors for arguments. For example, > > x<-rnorm(20,0,1) > sample(x,c(1,2,3)) ## will only return one random sample of size 1. > > The trick seems to be getting past the size argument of the function. I've > tried different things without success. Any help would be most appreciated! > > Thanks, > > A > > -- > View this message in context: http://r.789695.n4.nabble.com/Size-argument-in-sample-function-tp3605486p3605486.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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, There is nothing sacrosanct about taking a random sample of size 1 then 2 then 3 versus just taking one random sample of size 6 (with replacement, if you like) and then just use the first element, elements 2:3 and elements 4:6. HTH, Josh On Jun 17, 2011, at 6:59, alfredo <alfredotello at gmail.com> wrote:> Hi All, > > I'd like to randomly sample a vector N times, where each successive random > sample increases in size. I have realised that the function sample does not > take vectors for arguments. For example, > > x<-rnorm(20,0,1) > sample(x,c(1,2,3)) ## will only return one random sample of size 1. >Dat <- sample(x, 6) Dat[1] Dat[2:3] Dat[4:6]> The trick seems to be getting past the size argument of the function. I've > tried different things without success. Any help would be most appreciated! > > Thanks, > > A > > -- > View this message in context: http://r.789695.n4.nabble.com/Size-argument-in-sample-function-tp3605486p3605486.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.