R-listers

I am still finding my way with R - and feel that I am making a complete dogs
dinner of something that should be pretty simple.

What I'd like to do is to create a simple function that i can use to
calculate compound growth rates (CAGRs) over a data frame.  I'd like the
function to be flexible enough to vary the time period and to run over
'panel data'.

The issue is that I can get a function to work over one variable, but cant
figure out how to generalise it, specifically
... how do i get the original variable to be quoted in the output, not the
constructed variable in the function
... what is the most efficient way to loop over variables in the data set &
to output a labelled dataframe (potentially looping by variable, by time
period, or by region/country (more generally ... perhaps with plyr))

# example
require(ggplot2)
require(zoo)
require(xts)
data(economics)
test<-zoo(economics[,-1],economics$date)
cagr <- function(z,period) {
  z.sub <- last(xts(z),period)
  g <- lm(log(z.sub)~1+I(1:length(z.sub)))
  cagr <- (1+g$coeff[2])^(frequency(z.sub))-1
  print(cagr)
}

cagr(test$pce,"5 years")

grateful for any help

simeon

        [[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.

Reply via email to