문제

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