Skip to content
Prev 131743 / 398502 Next

Help rewriting looping structure?

Hi TLowe,

I'm not quite sure if I understand what you are trying to do.  If you 
are trying to get the cumulative sum of your data frame along each 
column you can simply do

rcumsum=function(x){cumsum(x)/sum(x)}
apply(tdat,2,rcumsum)

Yet that is not what your code is doing.  With a bit of clarification I 
may help you some more.

Julian
TLowe wrote:
for(j in 1:5) {
  tsum<- sum(tdat[,j]);
    for(k in 1:nrow(tdat)) {
      td<- tdat[k,j];
      tmp<-tdat[,j];
  ##### sum values <= to current value and divide by the total sum
    tdat[k,paste("cpct",j,sep="")]<- sum(tmp[tmp <= td]) / tsum;
    }
}