[R] Odp: Help with creating some loops

Petr PIKAL petr.pikal at precheza.cz
Fri Oct 30 15:01:35 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 30.10.2009 14:40:06:

> Hi All:
> 
> I have a data frame called all_corn. This has 31 columns. The first 
column is 
> a character key. The next 15 columns (stat1,stat2,...,stat15) are the 
> statistical forecast. The last 15 columns (sls1,sls2,...,sls5) are 
actual sales.
> I want to calculate textbook tracking signal and cuulative percent 
error.
> 
> 1) I am showing some of the calculations below. How can I make a loop 
out of 
> this instead of manually doing this 15 times?
> 2) Once All these calculations are done, how do I put all these columns 
> (err1,err2, etc.) into the same data frame?
> 
> Thanks.
> 

do not attach

> attach(all_corn)
> 

work with whole data frame instead columns

> cum_sls1 <- sls1
> err1 <- sls1-stat1

err <- all_corn[,17:31]-all_corn[,2:16]

err/all_corn[,17:31]*100
you can than handle all infs at once by is.finite

Regards
Petr

> cum_err1 <- sls1-stat1
> cum_abs_err1 <- abs(err1)
> mad1 <- abs(cum_err1)/1
> cum_pct_err1 <- (ifelse(cum_sls1 > 0, cum_err1/cum_sls1, 1))*100
> ts1 <- ifelse(mad1 > 0, cum_err1/mad1, 0)
> 
> cum_sls2 <- cum_sls1 + sls2
> err2 <- sls2-stat2
> cum_err2 <- cum_err1 + sls2-stat2
> cum_abs_err2 <- cum_abs_err1 + abs(err2)
> mad2 <- cum_abs_err2/2
> cum_pct_err2 <- (ifelse(cum_sls2 > 0, cum_err2/cum_sls2, 1))*100
> ts2 <- ifelse(mad2 > 0, cum_err2/mad2, 0)
> 
> ______________________________________________
> 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.




More information about the R-help mailing list