How do I rename the columns in a list of multi- series zoo objects?

StackOverflow https://stackoverflow.com/questions/18220695

  •  24-06-2022
  •  | 
  •  

Вопрос

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'))
Это было полезно?

Решение

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')
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top