1

My R learning curve has got the best of me today. So.. I have a list of multi series zoo objects. I'm trying to rename the columns in each to the same values. I'm attempting this in the last line... and it runs without error... but the names aren't changed. Any ideas would be great.

require("zoo")

Get monthly data of stocks.
symbs = c('AAPL', 'HOV', 'NVDA')

importData <- lapply(symbs, function(symb) get.hist.quote(instrument= symb, 
      start = "2000-01-01", end = "2013-07-15", quote="AdjClose", provider = "yahoo", 
      origin="1970-01-01", compression = "m", retclass="zoo"))
names(importData) <- symbs

#Calculate monthly pct chgs of stocks.
monthlyPctChgs = lapply(importData, function(x) diff(x, lag = 1) / lag(x, k = -1))
names(monthlyPctChgs) <- symbs

#Merge the pct chgs and the monthly closing prices
pricingAndPerfsMerged = mapply(merge, importData, lag(monthlyPctChgs, k = -1), 
      SIMPLIFY = FALSE)

#Rename the columns in each zoo.
lapply(pricingAndPerfsMerged, function(x) colnames(x) = c('AdjClose', 'MonthlyPerf'))
StatsViaCsh
  • 2,600
  • 10
  • 44
  • 63
  • You didn't assign the results to any object. – IRTFM Aug 14 '13 at 06:41
  • @DWin When I do, that doesn't actually rename them in my zoo objects... it just returns a list of 3, each with the name AAPL, HOV, or NVDA, with two empty columns (named correctly, at least). – StatsViaCsh Aug 14 '13 at 10:41

1 Answers1

1

You're renaming columns of a copy. This would be a good place to use a for loop instead:

for (i in seq_along(pricingAndPerfsMerged)) {
  colnames(pricingAndPerfsMerged[[i]]) = c('AdjClose', 'MonthlyPerf')
}
eddi
  • 49,088
  • 6
  • 104
  • 155
  • 1
    Really? I keep resisting the urge to just loop; I'm informed I need to use the apply family virtually anywhere I'd loop. OK. Loop it is. – StatsViaCsh Aug 13 '13 at 23:16
  • @StatsViaCsh [good reading on the general topic](http://stackoverflow.com/questions/7142767/why-are-loops-slow-in-r) – eddi Aug 13 '13 at 23:19