Re: [R] random sampling problems?

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Tue 04 Jul 2006 - 18:30:04 EST


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@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 04-Jul-06                                       Time: 09:30:00
------------------------------ XFMail ------------------------------

______________________________________________
R-help@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 Received on Tue Jul 04 18:37:14 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 04 Jul 2006 - 20:15:27 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.