Hi, I am trying to change a SAS macro to R. here is my code. I get an error at the last line. attach(fram) dset1<-cbind(AGE,BMI,DEATH)> BMIGRP<-cut(BMI,breaks=3,right=TRUE) > AGEGRP<-floor(AGE/10)-2 > dset<-cbind(AGEGRP,BMIGRP,DEATH) > maxage<-max(dset[,1]) > minage<-min(dset[,1]) > #maxcls<-dset[,2] > #mincls<-dset[,2] > nage<-maxage-minage+1 > nclass<-maxcls-mincls+1 > nsub<-nrow(dset) > weight <- matrix(,nage,1) > for ( i in minage:maxage )+ {weight[i-minage+1,1] = sum(std = i)}> > atrisk = matrix(,nclass,nage) > wevents = matrix(,nclass,nage) > #reduce data set to frequency table > for( i in (minage : maxage))+ for( j in (mincls : maxcls)) + #atrisk1<-aggregate(dset[,c("AGEGRP","BMIGRP")],list(RANDID=dset1$RANDID,sum) + + {atrisk[j-mincls+1,i-minage+1] + sum((dset[,1]=i)& (dset[,2]=j)) + wevents[j-mincls+1,i-minage+1]+ sum((dset[,1]=i)& (dset[,2]=j)&(dset[,3]=1))} Error: subscript out of bounds In addition: Warning messages: 1: numerical expression has 11627 elements: only the first used in: mincls:maxcls 2: numerical expression has 11627 elements: only the first used in: mincls:maxcls Any help will be greatly appreciated. Thanks, Anamika --------------------------------- [[alternative HTML version deleted]]
The lines> #maxcls<-dset[,2] > #mincls<-dset[,2]which you have shown commented out select a full column. You probably want the min and max of that column. With your definitions, mincls:maxlcs has the same type of behavior as (1:3):(1:3)
> x <- rnorm(100) > xx <- cut(x,3) > levels(xx)[1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]"> as.numeric(xx)
As my earlier email said I am not getting the maxcls. I do get numbers coded as 1,2,3 for BMIGRP(when I print BMIGRP) but not getting the max of (1,2,3) which should be 3, I guess. Thanks for your help Anamika Anamika Chaudhuri <canamika16@yahoo.com> wrote: > dset1<-cbind(AGE,BMI,DEATH)> BMIGRP<-cut(BMI,breaks=c(14,20,25,57),right=TRUE) > levels(BMIGRP)[1] "(14,20]" "(20,25]" "(25,57]"> BMIGRP1<-as.numeric(BMIGRP) > AGEGRP<-floor(AGE/10)-2 > dset<-cbind(AGEGRP,BMIGRP1,DEATH) > maxage<-max(dset[,1]) > minage<-min(dset[,1]) > maxcls<-max(dset[,2]) > maxcls[1] NA Why doesnt it give me a no for maxcls then? Thanks. "Richard M. Heiberger" <rmh@temple.edu> wrote: > x <- rnorm(100)> xx <- cut(x,3) > levels(xx)[1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]"> as.numeric(xx)--------------------------------- --------------------------------- [[alternative HTML version deleted]]
Hi you experienced difference between matrix (cbind(....)) and data frame (data.frame(......)) or you have some NA value in your BMIGRP1 try: maxcls<-max(dset[,2], na.rm=T) if you have some NA values or use str(dset) to look what types are your variables. HTH Petr On 25 Apr 2006 at 18:16, Anamika Chaudhuri wrote: Date sent: Tue, 25 Apr 2006 18:16:42 -0700 (PDT) From: Anamika Chaudhuri <canamika16 at yahoo.com> To: "Richard M. Heiberger" <rmh at temple.edu>, r-help at stat.math.ethz.ch Subject: Re: [R] Help needed> As my earlier email said I am not getting the maxcls. I do get numbers > coded as 1,2,3 for BMIGRP(when I print BMIGRP) but not getting the max > of (1,2,3) which should be 3, I guess. > > Thanks for your help > Anamika > > > Anamika Chaudhuri <canamika16 at yahoo.com> wrote: > > dset1<-cbind(AGE,BMI,DEATH) > > BMIGRP<-cut(BMI,breaks=c(14,20,25,57),right=TRUE) > > levels(BMIGRP) > [1] "(14,20]" "(20,25]" "(25,57]" > > BMIGRP1<-as.numeric(BMIGRP) > > AGEGRP<-floor(AGE/10)-2 > > dset<-cbind(AGEGRP,BMIGRP1,DEATH) > > maxage<-max(dset[,1]) > > minage<-min(dset[,1]) > > maxcls<-max(dset[,2]) > > maxcls > [1] NA > > Why doesnt it give me a no for maxcls then? > > Thanks. > > > "Richard M. Heiberger" <rmh at temple.edu> wrote: > > x <- rnorm(100) > > xx <- cut(x,3) > > levels(xx) > [1] "(-2.37,-0.716]" "(-0.716,0.933]" "(0.933,2.58]" > > as.numeric(xx) > > > --------------------------------- > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz