简体   繁体   中英

Data.frame - combine rows

I am trying to set data.frame for some stocks' price, but I get the following error:

Error in data.frame(AAPL, AMZN, AXP, VW) : arguments imply differing number of rows: 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.

Fortunately, R has a few excellent packages for those working with financial data to help you get started. As a reproducible example, download data using quantmod , which automatically creates xts objects for each stock, complete with a date/time index.

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.

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

If you'd like a data.frame after that:

stocksDF <- data.frame(stocksXTS)

That's it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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