Help rewriting looping structure?
Is this basically what you want to do? (Please include commented, minimal, self-contained, reproducible code so we don't have to guess at what you want)
x <- data.frame(a=runif(10), b=runif(10))
# do for one column
cumsum(x$a)/sum(x$a)
[1] 0.05892073 0.08129611 0.11067218 0.28640268 0.28969826 0.44477544 0.55195101 0.76500220 0.85234025 [10] 1.00000000
If this is the case, you can extend it.
On Dec 6, 2007 9:02 AM, TLowe <rcl7820 at warnell.uga.edu> 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#a14196412
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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?