Dear friends, suppose my dataset is the following data: id<-1:9 x<-c(1,2,3,1,2,3,1,2,3) y<-c(1,1,1,2,2,2,3,3,3) data<-data.frame(id,x,y) id x y 1 1 1 1 2 2 2 1 3 3 3 1 4 4 1 2 5 5 2 2 6 6 3 2 7 7 1 3 8 8 2 3 9 9 3 3 i want to do sampling like this:say the sample size is 3. First: random sampling from x; Next ,random sampling from y ;and combing sampled x and sampled y; Finally, output the samples: id x and y. I think i could call it two-dimension sampling. Thanks very much! -- Kind Regards, Zhi Jie,Zhang ,PHD Department of Epidemiology School of Public Health Fudan University Tel:86-21-54237149 [[alternative HTML version deleted]]
with replacement or not ? without replacement: data1 <- cbind(id=1:9, expand.grid(x=1:3,y=1:3)) merge(data1, sapply(data1[,c("x","y")], sample, 3), all.y=T) why not: data1[sample(data1$id, 3),] ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- zhijie zhang a ?crit :> Dear friends, > suppose my dataset is the following data: > > id<-1:9 > x<-c(1,2,3,1,2,3,1,2,3) > y<-c(1,1,1,2,2,2,3,3,3) > data<-data.frame(id,x,y) > > id x y > 1 1 1 1 > 2 2 2 1 > 3 3 3 1 > 4 4 1 2 > 5 5 2 2 > 6 6 3 2 > 7 7 1 3 > 8 8 2 3 > 9 9 3 3 > i want to do sampling like this:say the sample size is 3. > First: random sampling from x; > Next ,random sampling from y ;and combing sampled x and sampled y; > Finally, output the samples: id x and y. > I think i could call it two-dimension sampling. > Thanks very much! > > >
On 04-Jul-06 zhijie zhang wrote:> Dear friends, > suppose my dataset is the following data: > > id<-1:9 > x<-c(1,2,3,1,2,3,1,2,3) > y<-c(1,1,1,2,2,2,3,3,3) > data<-data.frame(id,x,y) > > id x y > 1 1 1 1 > 2 2 2 1 > 3 3 3 1 > 4 4 1 2 > 5 5 2 2 > 6 6 3 2 > 7 7 1 3 > 8 8 2 3 > 9 9 3 3 > i want to do sampling like this:say the sample size is 3. > First: random sampling from x; > Next ,random sampling from y ;and combing sampled x and sampled y; > Finally, output the samples: id x and y. > I think i could call it two-dimension sampling. > Thanks very much!I'm not quite sure what you are asking, but perhaps you can confirm that it corresponds to the following, which is what you seem to mean according to your wording. You want an output which consists of the following: a random sample of 3 from the list of values of x an independent random sample of 3 from the list of values of y the list of the id values for the sampled values of x the list of the id values for the sampled values of y where the sampling is without replacement (default for "sample") If that is the case, then idx<-sample(data$id,3) idx # [1] 6 8 9 idy<-sample(data$id,3) idy # [1] 7 2 3 sampx<-data$x[idx] sampx # [1] 3 2 3 sampy<-data$y[idy] sampy # [1] 3 1 1 samples<-data.frame(sampx,sampy,idx,idy) samples # sampx sampy idx idy # 1 3 3 6 7 # 2 2 1 8 2 # 3 3 1 9 3 Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 04-Jul-06 Time: 09:30:00 ------------------------------ XFMail ------------------------------
Reasonably Related Threads
- a question on subset a dataset
- Getting polygons from contiguous grid cells having same value in sp
- fitting a model with the nlme package
- help with calculation from dataframe with multiple entries per sample
- merging two data frame with colomns of different length