[R] random sampling problems?
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Tue Jul 4 10:30:04 CEST 2006
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 ------------------------------
More information about the R-help
mailing list