1

I am downloading data from FRED with getSymbols. This creates an xts class object with the data attribute set to type integer for the series' that I am downloading. I want these data to be of type/class double. What is an idiomatic way of doing this?

  getSymbols("GDPMC1", src = 'FRED', auto.assign = TRUE)
  growthRate <-
      function (x) {
      stopifnot(length(x) > 1)
      (x[2:length(x)] - x[-length(x)] )/ x[-length(x)]
    }
 stopifnot(growthRate(c(2,3,4)) == c(0.5 , 1/3 ))
 realGDPGrowthRate <- growthRate(GDPMC1) ### zeros due to integer math
mcheema
  • 850
  • 9
  • 25

1 Answers1

3

You can change the storage mode for GDPMC1 to "double" via:

storage.mode(GDPMC1) <- "double"

But that won't solve your problem because the issue isn't integer arithmetic. The issue is that xts/zoo align objects by index before performing any Ops methods (arithmetic, logical operations, etc), so your growthRate function will never work correctly on xts/zoo objects.

You can use quantmod's Delt function instead of writing your own.

realGDPGrowthRate <- Delt(GDPMC1)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418