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. attach(all_corn) cum_sls1 <- sls1 err1 <- sls1-stat1 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)
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 firstcolumn is> a character key. The next 15 columns (stat1,stat2,...,stat15) are the > statistical forecast. The last 15 columns (sls1,sls2,...,sls5) areactual sales.> I want to calculate textbook tracking signal and cuulative percenterror.> > 1) I am showing some of the calculations below. How can I make a loopout 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-stat1err <- 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi: In general, how to I cast a character to the class that I am trying to change. For example, if I have a data frame df1. df1 has a column x. suppose I want to a substring of x (the first 3 chars). Then I want to do something like df1$new = substring(of x) Example Data frame df1 x abcd efgh Now df1$new should be ab ef Thanks. Satish _____________________________________________ From: Vadlamani, Satish {FLNA} Sent: Friday, October 30, 2009 8:40 AM To: R-help at r-project.org Subject: Help with creating some loops 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. attach(all_corn) cum_sls1 <- sls1 err1 <- sls1-stat1 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)
On Oct 30, 2009, at 11:24 AM, Vadlamani, Satish {FLNA} wrote:> Hi: > In general, how to I cast a character to the class that I am trying > to change. > > For example, if I have a data frame df1. df1 has a column x. suppose > I want to a substring of x (the first 3 chars). Then I want to do > something like > > df1$new = substring(of x) > > Example > Data frame df1 > x > abcd > efgh > > Now df1$new should be > ab > efFollowing your written specification (rather than your example): > df1 <- data.frame(x=c("abcd", "efgh")) > df1 x 1 abcd 2 efgh > df1$new <- substr(df1$x,start=1,stop=3) > df1 x new 1 abcd abc 2 efgh efg> > Thanks. > Satish > > > > _____________________________________________ > From: Vadlamani, Satish {FLNA} > Sent: Friday, October 30, 2009 8:40 AM > To: R-help at r-project.org > Subject: Help with creating some loops > > 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. > > attach(all_corn) > > cum_sls1 <- sls1 > err1 <- sls1-stat1 > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT