[R] Sampling problem
Michael Bedward
michael.bedward at gmail.com
Tue Nov 16 03:18:49 CET 2010
Hello,
Is this what you want ?
sampleX <- function(X, nGrp1, nsamples)
# X is matrix or data.frame with cols for two groups of variables
# with grp1 in cols 2:5 and grp2 in cols 6:9
#
# nGrp1 <- number of variables to sample from group 1
#
# nsamples <- number of rows in output matrix
if (nGrp1 >= 4) stop("can't sample all group 1 variables")
out <- matrix(0, nsamples, nGrp1+1)
for (i in 1:nsamples) {
# choose grp1 vars to sample
grp1 <- sample(4, nGrp1)
# choose complentary grp2 var to sample
grp2 <- sample((1:4)[-grp1], 1)
# sample 1 value from each var
out[i, ] <- apply(X[,c(grp1+1, grp2+5)], 2, sample, 1)
}
out
}
Michael
On 16 November 2010 07:59, wangwallace <talenttree at gmail.com> wrote:
>
> Hey,
>
> I am hoping someone can help me with a sampling question.
>
> I have a data frame of 8 variables (the first column is the subjects' id):
>
> SubID CSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4
> 1 6 5 6 2 6 2 2 4
> 2 6 4 7 2 6 6 2 3
> 3 5 5 5 5 5 5 4 5
> 4 5 4 3 4 4 4 5 2
> 5 5 6 7 5 6 4 4 1
> 6 5 4 3 6 4 3 7 3
> 7 3 6 6 3 6 5 2 1
> 8 3 6 6 3 6 5 4 7
>
> the 6 variables are categorized into two groups with CSE1, CSE2, CSE3, and
> CSE4 in one group and the rest in another group.
>
>>sample(data[,2:4],2,replace=FALSE)
>
> CSE1 CSE2
> 1 6 5
> 2 6 4
> 3 5 5
> 4 5 4
> 5 5 6
> 6 5 4
> 7 3 6
> 8 3 6
>
> Now I want to sample 1 column from another group of variables (i.e., WSE1,
> WSE2, WSE3, WSE4), but I want to restrict a vector I am going to sample from
> to only those columns that are not correspond to GROUP 1 variables I have
> sampled. That is, I want to sample a column from WSE3, WSE4 Columns
> corresponding to CSE1 and CSE2 (i.e., WSE1, WSE2) need to be dropped.
>
> How can I do this? what if I want to repeat this whole process (drawing 2
> random columns from CSE1, CSE2, CSE3, and CSE4 first, AND then another
> random column from WSE1, WSE2, WSE3, and WSE4) for 1000 times. any ideas?
>
> Many thanks in advance!!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3043804.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.
>
More information about the R-help
mailing list