Re: [R] Help rewriting looping structure?

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Thu, 06 Dec 2007 12:20:19 -0600

How about this example?

## sample data frame with two columns
df <- data.frame(x = abs(rnorm(20)), y=abs(rnorm(20,2)))

## create new variables in df with an lapply call df[c("cpctx","cptcty")] <- lapply(df, function(x) cumsum(x)/sum(x))

A possible improvement would be to construct the new column names in the data frame automatically.

Best,
Erik
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



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. Received on Thu 06 Dec 2007 - 18:23:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Dec 2007 - 19:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.