[R] error message
wangwallace
talenttree at gmail.com
Tue Jan 18 17:32:08 CET 2011
I was running a sampling syntax based on a data frame (ago) of 160 rows and
25 columns. Below are the column names:
> names(ago)
[1] "SubID" "AGR1" "AGR2" "AGR3" "AGR4" "AGR5" "AGR6" "AGR7"
"AGR8"
[10] "AGR9" "AGR10" "WAGR1" "WAGR2" "WAGR3" "WAGR4" "WAGR5" "WAGR6"
"WAGR7"
[19] "WAGR8" "WAGR9" "WAGR10" "ocbi" "ocbo" "cwbi" "cwbo"
> dim(ago)
[1] 160 25
Here below is the syntax:
> crossed1<-function(df){
+ s1<-df[sample(nrow(df),16,replace=F),]
+ s2<-t(apply(s1[,2:11],1,sample,10))
+ s2<-data.frame(s2)
+ s3<-df[-s1$SubID,]
+ ind<-t(replicate(nrow(s3),sample(10)))
+ s4<-matrix(nrow=nrow(s3),ncol=10)
+ rownames(s4)<-rownames(s3)
+ s4<-data.frame(s4)
+ for(i in seq(nrow(s3))){
+ s4[i,1]<-s3[i,1+ind[i,1]]
+ s4[i,2:10]<-s3[i,11+ind[i,2:10]]
+ }
+ colnames(s2)<-colnames(s4)
+ rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4)))),]
+ }
> result1<-vector("list",1000)
> for(i in 1:1000)result1[[i]]<-crossed1(ago)
These syntaxes worked out perfectly. I successfully drew 1000 random
samples.
I applied the syntax above to another data set called agr, which basically
has the same column names:
> names(agr)
[1] "SubID" "AGR1" "AGR2" "AGR3" "AGR4" "AGR5" "AGR6" "AGR7"
"AGR8"
[10] "AGR9" "AGR10" "WAGR1" "WAGR2" "WAGR3" "WAGR4" "WAGR5" "WAGR6"
"WAGR7"
[19] "WAGR8" "WAGR9" "WAGR10" "ocbi" "ocbo" "cwbi" "cwbo"
agr and ago are basically the same except that agr has more rows than ago:
> dim(agr)
[1] 300 25
here below is the same syntax I've used for agr:
> crossed1<-function(df){
+ s1<-df[sample(nrow(df),30,replace=F),] # note I have changed 16 to 30
+ s2<-t(apply(s1[,2:11],1,sample,10))
+ s2<-data.frame(s2)
+ s3<-df[-s1$SubID,]
+ ind<-t(replicate(nrow(s3),sample(10)))
+ s4<-matrix(nrow=nrow(s3),ncol=10)
+ rownames(s4)<-rownames(s3)
+ s4<-data.frame(s4)
+ for(i in seq(nrow(s3))){
+ s4[i,1]<-s3[i,1+ind[i,1]]
+ s4[i,2:10]<-s3[i,11+ind[i,2:10]]
+ }
+ colnames(s2)<-colnames(s4)
+ rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4)))),]
+ }
> result1<-vector("list",1000)
> for(i in 1:1000)result1[[i]]<-crossed1(agr)
Error in xj[i] : only 0's may be mixed with negative subscripts
However, I got an error message shown above. It is kinda weird. Could
anybody please explain what I've did wrong? Many thanks in advance!! :)
--
View this message in context: http://r.789695.n4.nabble.com/error-message-tp3223412p3223412.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list