[R] Help rewriting looping structure?
TLowe
rcl7820 at warnell.uga.edu
Thu Dec 6 19:28:10 CET 2007
Thank you all. That's exactly what I was looking for.
TLowe wrote:
>
> Hey Folks,
>
> Could somebody help me rewrite the following code?
>
> I am looping through all records across 5 fields to calculate the
> cumulative
> percentage of each record (relative to each individual field).
>
> Is there a way to rewrite it so I don't have to loop through each
> individual
> record?
>
> ##### tdat is my data frame
> ##### j is my field index
> ##### k is my record index
> ##### tsum is the sum of all values in field j
> ##### tmp is a vector containing the values in field j
> ##### tdat[k,paste("cpct,j,sep="")] creates new fields "cpct1",...,"cpct5"
>
>
> 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;
> }
> }
>
>
> Thanks,
> TLowe
>
--
View this message in context: http://www.nabble.com/Help-rewriting-looping-structure--tf4957267.html#a14198294
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list