如何重命名多系列动物园对象列表中的列?

时间:2013-08-13 23:05:16

标签: r zoo

我的R学习曲线今天对我来说是最好的。那么..我有一系列多系列动物园对象。我正在尝试将每个列中的列重命名为相同的值。我在最后一行尝试这个...它运行没有错误......但名称没有改变。任何想法都会很棒。

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'))

1 个答案:

答案 0 :(得分:0)

您正在重命名副本的列。这将是一个使用for循环的好地方:

for (i in seq_along(pricingAndPerfsMerged)) {
  colnames(pricingAndPerfsMerged[[i]]) = c('AdjClose', 'MonthlyPerf')
}