Very new to R, so I hope I don't frustrate anyone.
Putting together pieces from online searches and using quantmode and purr packages, I have the following code to create an xts data frame called stocks
:
symbols <- c("RYCVX","AJA","IEMG")
start <- as.Date("2006-06-22")
end <- as.Date("2020-07-30")
# collect adjusted column of all symbols in one matrix
stocks <- getSymbols(symbols,src = "yahoo", from = start, to = end,
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
This is daily, but I want to have a monthly matrix, yet still keep the NA
fields.
I tried this line of code:
mstocks <- to.monthly(stocks, indexAt = "last", OHLC = FALSE)
but my resulting data frame is shrunk down to the symbol with the least amount of data, since any row with any missing value is omitted, so I end losing data on the more historically rich symbol.
Is there a way I could keep the missing values and have monthly data that, like my daily data, has rows where one symbol is NA
?
So here is what I get:
RYCX AJA IEMG
2018-12-30 29.3045 4.5523 33.2045 <- first date all symbols have data
...
2020-07-30 34.2344 5.6664 12.2234
What I get now with Walts's help:
V1
2006-06-30 NA
...
2020-07-29 52.66000
What I need:
RYCX AJA IEMG
2006-06-30 29.3045 NA NA
....
2020-07-30 34.2344 5.6664 12.2234
All prices are made up