简体   繁体   English

Data.frame-合并行

[英]Data.frame - combine rows

I am trying to set data.frame for some stocks' price, but I get the following error: 我正在尝试为某些股票的价格设置data.frame ,但是出现以下错误:

Error in data.frame(AAPL, AMZN, AXP, VW) : arguments imply differing number of rows: 1259, 1021 data.frame(AAPL,AMZN,AXP,VW)中的错误:参数暗示行数不同:1259、1021

How can combine these two objects when they have a different number of rows? 当两个对象的行数不同时,如何合并?

Your probably getting the error because you are attempting to combine vectors of stock prices which are of differing lengths. 您可能会收到此错误,因为您正试图合并长度不同的股票价格向量。 AKA some stocks in your data set have more history than others. 也就是您数据集中的某些股票比其他股票具有更多的历史记录。

Further, because these are time series data, you really should have a date/time index associated with each stock so that you can merge them all by their respective date/time index key. 此外,由于这些是时间序列数据,因此您确实应该有与每个股票相关联的日期/时间索引,以便可以通过它们各自的日期/时间索引键merge它们全部merge

Fortunately, R has a few excellent packages for those working with financial data to help you get started. 幸运的是,R为处理财务数据的人员提供了一些出色的软件包,以帮助您入门。 As a reproducible example, download data using quantmod , which automatically creates xts objects for each stock, complete with a date/time index. 作为可重现的示例,使用quantmod下载数据,该工具自动为每只股票创建xts对象,并带有日期/时间索引。

stocks <- c("AAPL", "AMZN", "AXP", "VW") 

library(quantmod)
getSymbols(stocks)

Next, combine your stocks using the merge.xts which automatically joins each by date index. 接下来,使用merge.xts合并您的股票,该merge.xts会自动按日期索引合并每个股票。

stocksXTS <- merge.xts(AAPL, AMZN, AXP, VW)

If you'd like a data.frame after that: 如果之后需要data.frame

stocksDF <- data.frame(stocksXTS)

That's it! 而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM