[R] recursive function

K. Elo maillists at nic.fi
Mon Jun 14 19:56:13 CEST 2010


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:

> df1
  SERIES 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



More information about the R-help mailing list