Dear list, I have the following problem, what i'm trying to do is to built a function which does the following calculationg in a recursive way: I have a data frame more or less like this: variable year DELTA EC01 2006 / EC01 2007 10 EC01 2008 5 EC01 2009 9 And then I have at time 2009 a variable called R_EC01(2009)=5 What I have to do is to construct the R_EC01 time series by starting from the 2009 value: R_EC01(2008)=R_EC01(2009)-DELTA(2009) R_EC01(2007)=R_EC01(2008)-DELTA(2008) R_EC01(2006)=R_EC01(2007)-DELTA(2007) In terms of number, the results that i should get are: R_EC01(2008)=5-9=-4 R_EC01(2007)=-4-5=-9 R_EC01(2006)=-9-10=-19 so my data frame should looks like this SERIES YEAR value R_EC01 2006 -19 R_EC01 2007 -9 R_EC01 2008 -4 R_EC01 2009 5 Anyone Knows hot to do it?? My dataframe is not set as a time series... Thanks a lot!!! [[alternative HTML version deleted]]
Hi! Do you mean something like this (df is your original data frame): --- cut here --- df1<-df df1[[1]]<-paste("R",df[[1]],sep="_") colnames(df1)<-c("SERIES","YEAR","value") df1$value[ df1$YEAR==2009 ]<-5 for (i in c(2009:2007)) { df1$value[ df1$YEAR==(i-1) ]<-( df1$value[ df1$YEAR==i ]-df$DELTA[ df$year==i ] ) } --- cut here --- Now the output:> df1SERIES YEAR value 1 R_EC01 2006 -19 2 R_EC01 2007 -9 3 R_EC01 2008 -4 4 R_EC01 2009 5 Please let me know if you were looking for a more general approach suitable for larger data frames with e.g. several "variable" classes (EC01, EC02 etc.) Kind regards, Kimmo -- University of Turku, Finland Dep. of Political Science and Contemporary history
Try this: transform(x, DELTA = NULL, value = rev(c(5, 5 - cumsum(rev(DELTA[-1]))))) On Mon, Jun 14, 2010 at 12:29 PM, n.vialma@libero.it <n.vialma@libero.it>wrote:> > Dear list, > I have the following problem, what i'm trying to do is to built a function > which does the following calculationg in a recursive way: > > > I have a data frame more or less like this: > > variable year DELTA > > EC01 2006 / > EC01 2007 10 > EC01 2008 5 > EC01 2009 9 > > > And then I have at time 2009 a variable called R_EC01(2009)=5 > What I have to do is to construct the R_EC01 time series by starting from > the 2009 value: > R_EC01(2008)=R_EC01(2009)-DELTA(2009) > R_EC01(2007)=R_EC01(2008)-DELTA(2008) > R_EC01(2006)=R_EC01(2007)-DELTA(2007) > > > In terms of number, the results that i should get are: > R_EC01(2008)=5-9=-4 > > R_EC01(2007)=-4-5=-9 > R_EC01(2006)=-9-10=-19 > so my data frame should looks like this > SERIES YEAR value > > R_EC01 2006 -19 > > R_EC01 2007 -9 > > R_EC01 2008 -4 > > R_EC01 2009 5 > Anyone Knows hot to do it?? > My dataframe is not set as a time series... > > > Thanks a lot!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]