[R] error message
Adrienne Wootten
amwootte at ncsu.edu
Tue Jan 18 21:09:20 CET 2011
It's not immediately evident to me just looking at this, but I do have
one suggestion. Run this again and see if you get the same error and
if you do, use this command to help you find where -
traceback()
traceback is extremely helpful when you are debugging these things, it
will go through your function and find where your function crashed if
not why exactly.
Give it a try.
A
--
Adrienne Wootten
Graduate Research Assistant
State Climate Office of North Carolina
Department of Marine, Earth and Atmospheric Sciences
North Carolina State University
On Tue, Jan 18, 2011 at 11:32 AM, wangwallace <talenttree at gmail.com> wrote:
>
> 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.
>
> ______________________________________________
> 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