[R] Odp: Sampling problem
Petr PIKAL
petr.pikal at precheza.cz
Tue Nov 16 17:09:07 CET 2010
Hi
Here is one way (If I understood what you did ask).
test<-read.table("clipboard", header=T)
> test
SubID CSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4
1 1 6 5 6 2 6 2 2 4
2 2 6 4 7 2 6 6 2 3
3 3 5 5 5 5 5 5 4 5
4 4 5 4 3 4 4 4 5 2
5 5 5 6 7 5 6 4 4 1
6 6 5 4 3 6 4 3 7 3
7 7 3 6 6 3 6 5 2 1
8 8 3 6 6 3 6 5 4 7
fff<-function(dat, col1=2, col2=1) {
# col1 are number of columns from fist set and col2 from the second set
sel1<-sample(1:4, col1)
sel2<-sample((1:4)[-sel1], col2)
dat[,c(sel1+1,sel2+5)]
# i presume that your data are same as you posted, if not you has to
change above values
}
fff(test)
CSE2 CSE1 WSE3
1 5 6 2
<snip>
8 6 3 4
> fff(test)
CSE1 CSE2 WSE3
1 6 5 2
<snip>
8 3 6 4
> fff(test)
CSE1 CSE3 WSE4
1 6 6 4
<snip>
8 3 6 7
If you want to do it 1000 times just use simple loop
result <- vector("list", 1000)
for (i in 1:1000) result[[i]] <- fff(test)
Regards
Petr
r-help-bounces at r-project.org napsal dne 15.11.2010 21:59:21:
> wangwallace <talenttree at gmail.com>
> Odeslal: r-help-bounces at r-project.org
>
> 15.11.2010 21:59
>
> Komu
>
> r-help at r-project.org
>
> Kopie
>
> Předmět
>
> [R] Sampling problem
>
>
> 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