0
votes

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?

1
Maybe you can provide a little more information about what you're trying to do? What do you mean by "combine with different rows"? - jfkoehler
Don't combine as different columns. Stack those vertically and use a column (as a factor) to denote a ticker. - mlt
If it's quantmod they have a merge function in the package - Vivek Katial

1 Answers

0
votes

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!