For循环重命名许多对象的列名R

For Loop to Rename Column Names of Many Objects R

我正在寻找一种使用 for 循环或 R 中的其他方法重命名多个对象的列的方法。最终,我希望能够将每个 Stock 对象的行绑定到一个大数据框中,但是不能由于不同的列名。示例如下:

AAPL <-
Date        AAPL.Open  AAPL.High AAPL.Low  AAPL.Close  AAPL.Volume  AAPL.Adjusted  Stock  pct_change
2020-05-14  304.51     309.79    301.53    309.54      39732300     309.54         AAPL   0.61
2020-05-15  300.35     307.90    300.21    307.71      41561200     307.71         AAPL  -0.59


GOOG <-  
Date        GOOG.Open GOOG.High  GOOG.Low  GOOG.Close  GOOG.Volume   GOOG.Adjusted  Stock  pct_change 
2020-05-14  1335.02   1357.420   1323.910  1356.13     1603100       1356.13        GOOG   0.50
2020-05-15  1350.00   1374.480   1339.000  1373.19     1705700       1373.19        GOOG   1.26

对于这个示例,我有 2 个对象(AAPL 和 GOOG),但实际上我会使用更多对象。我可以创建一个 for 循环来遍历每个对象,并将每个对象的第 2 列重命名为 "Open",第 3 列重命名为 "High",第 4 列重命名为 "Low",...等等然后我可以将所有这些对象绑定在一起吗?

我已经有一个名为 "Stock" 的列,所以我不需要列名称的代码部分。

如果您可以保证这些列的顺序,那么应该这样做:

for(df in list(AAPL, GOOG))
  colnames(df) <- c("Date", "Open", "High", "Low", "Close", "Volume", "Adjusted", "Stock", "pct_change")

使用 lapply,我们可以遍历 list 并删除带有 sub 的列名称中的前缀。这可以在没有任何外部包的情况下完成

lst1 <- lapply(list(AAPL, GOOG),  function(x)  {
        colnames(x) <- sub(".*\.", "", colnames(x))
    x})

使用 quantmod 我们可以读取一组股票代码,将它们的名称清理并 rbind() 到一个数据框中。

此答案说明了三个主要特征,包括:

  1. 使用 get() 访问 quantmod::getSymbols() 写入的 object 加载到内存后。

  2. 使用传递给 lapply() 的符号名称向每个数据框添加一个 symbol 列。

  3. getSymbols() 写入的 xts object 中存储为行名称的日期转换为数据框列。

首先,我们将使用 getSymbols() 从 yahoo.com 读取数据。

library(quantmod)
from.dat <- as.Date("12/02/19",format="%m/%d/%y")
to.dat <- as.Date("12/06/19",format="%m/%d/%y")

theSymbols <- c("AAPL","AXP","BA","CAT","CSCO","CVX","XOM","GS","HD","IBM",
                "INTC","JNJ","KO","JPM","MCD","MMM","MRK","MSFT","NKE","PFE","PG",
                "TRV","UNH","UTX","VZ","V","WBA","WMT","DIS","DOW")
getSymbols(theSymbols,from=from.dat,to=to.dat,src="yahoo")

# since quantmod::getSymbols() writes named data frames, need to use
# get() with the symbol names to access each data frame
head(get(theSymbols[[1]]))



> head(get(theSymbols[[1]]))
           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2019-12-02    267.27    268.25   263.45     264.16    23621800      262.8231
2019-12-03    258.31    259.53   256.29     259.45    28607600      258.1370
2019-12-04    261.07    263.31   260.68     261.74    16795400      260.4153
2019-12-05    263.79    265.89   262.73     265.58    18606100      264.2359

说明了如何在全局环境中访问符号 objects,我们将使用 lapply() 从行名称中提取日期,清理列标题,然后写入符号名称作为每个交易品种数据的列 object。

# convert to list
symbolData <- lapply(theSymbols,function(x){
     y <- as.data.frame(get(x))
     colnames(y) <- c("open","high","low","close","volume","adjusted")
     y$date <- rownames(y)
     y$symbol <- x
     y
})

最后,我们将数据帧列表转换为单个数据帧。

#combine to single data frame
combinedData <- do.call(rbind,symbolData)
rownames(combinedData) <- 1:nrow(combinedData)

...和输出:

> nrow(combinedData)
[1] 120
> head(combinedData)
    open   high    low  close   volume adjusted       date symbol
1 267.27 268.25 263.45 264.16 23621800 262.8231 2019-12-02   AAPL
2 258.31 259.53 256.29 259.45 28607600 258.1370 2019-12-03   AAPL
3 261.07 263.31 260.68 261.74 16795400 260.4153 2019-12-04   AAPL
4 263.79 265.89 262.73 265.58 18606100 264.2359 2019-12-05   AAPL
5 120.31 120.36 117.07 117.26  5538200 116.2095 2019-12-02    AXP
6 116.04 116.75 114.65 116.57  3792300 115.5256 2019-12-03    AXP
>