[R] Sampling problem
wangwallace
talenttree at gmail.com
Tue Nov 16 17:01:48 CET 2010
yes, the data.frame is exactly the same as the one I posted earlier.
I was trying to see if the loop function works. And I got that message. here
below is the syntax I was trying to run, followed by the error message at
the end:
> sampleX<-function(X,nGrp1,nsamples){if(nGrp1>=4)stop("can't sample all
> group 1 variables")
+ out<-matrix(0,nsamples,nGrp1+1)
+ for(i in 1:nsamples){
+ grp1<-sample(4,nGrp1)
+ grp2<-sample((1:4)[-grp1],)
+ out[i,]<-apply(X[,c(grp1+1,grp2+5)],2,sample,1)
+ }
+ out}
> sampleX(help,1,10)
Error in out[i, ] <- apply(X[, c(grp1 + 1, grp2 + 5)], 2, sample, 1) :
number of items to replace is not a multiple of replacement length
By the way, it is only a small piece of my data set, which has 12 variables
(or columns) for each group (grp1: CSE1, CSE2, CSE3, CSE4, CSE5, CSE6, CSE7,
CSE8, CSE9, CSE10, CSE11, CSE12; grp2: WSE1, WSE2, WSE3, WSE4, WSE5, WSE6,
WSE7, WSE8, WSE9, WSE10, WSE11, WSE12). I will draw 1000 random samples for
each of the 11 different combinations below:
combination 1: 1 variable from grp1 + 11 variables from grp2 = 12 variables
combination 2: 2 variable from grp1 + 10 variables from grp2 = 12 variables
combination 3: 3 variable from grp1 + 9 variables from grp2 = 12 variables
combination 4: 4 variable from grp1 + 8 variables from grp2 = 12 variables
combination 5: 5 variable from grp1 + 7 variables from grp2 = 12 variables
combination 6: 6 variable from grp1 + 6 variables from grp2 = 12 variables
combination 7: 7 variable from grp1 + 5 variables from grp2 = 12 variables
combination 8: 8 variable from grp1 + 4 variables from grp2 = 12 variables
combination 9: 9 variable from grp1 + 3 variables from grp2 = 12 variables
combination 10: 10 variable from grp1 + 2 variables from grp2 = 12 variables
combination 11: 11 variable from grp1 + 1 variables from grp2 = 12 variables
As shown above, the sum of the variables in each combination will have to be
12. Also, I want to restrict a vector I am going to sample from to only
those columns that are not correspond to grp1 variables I have sampled. For
example, if I sampled 1 variable, say CSE1, from grp1, the other 11
variables from grp2 should not include WSE1; if I sampled 2 variables, say
CSE1 and CSE2, from grp1, the other 10 variables from grp2 should not
include WSE1 and WSE2.
Anyway, this is a lot more complicated example than the one I described in
my first post. But I think I can modify your function if I wanna apply it to
the large data set with 12 variables for each group, since they basically
share the same method. Now I am wondering where the error message is from.
Again, thanks!! :)
--
View this message in context: http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3045095.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list