I have multiple data.frame
objects of unequal lengths. I would like to find the most recent date in all of them and store the data somewhere.
Here is an example of hopefully reproducible code to illustrate what I would like (with comments and sources). This gives 7 data.frame
objects of variable lengths:
library(quantmod)
# Load ticker data from 2020-01-01 till 2021-02-02
tickers <- c("NKLA", "MPNGF", "RMO", "JD", "COIN")
getSymbols.yahoo(tickers, auto.assign = TRUE, env = globalenv(), from = "2020-01-01", to = "2021-02-02")
# Load ticker data from 2020-01-01 till yesterday (if not weekend or holiday)
tickers2 <- c("IBM", "AAPL", "MRNA")
getSymbols.yahoo(tickers2, auto.assign = TRUE, env = globalenv(), from = "2020-01-01")
# Close all Internet connections as a precaution
# https://stackoverflow.com/a/52758758/2950721
closeAllConnections()
# Find xts objects
xtsObjects <- names(which(unlist(eapply(.GlobalEnv, is.xts))))
# Convert xts to data.frame
# https://stackoverflow.com/a/69246047/2950721
for (i in seq_along(xtsObjects)) {
assign(xtsObjects[i], fortify.zoo(get(xtsObjects[i])))
}
# 1st column name from Index to Date
# https://stackoverflow.com/a/69292036/2950721
for (i in seq_along(xtsObjects)) {
tmp <- get(xtsObjects[i])
colnames(tmp)[colnames(tmp) == "Index"] <- "Date"
assign(xtsObjects[i], tmp)
}
remove(tmp)
Individually retreive the dates is pretty straightforward:
max(AAPL$Date)
max(IBM$Date)
max(JD$Date)
max(MPNGF$Date)
max(MRNA$Date)
max(NKLA$Date)
max(RMO$Date)
But when I try the following codes none of them would render or, better yet, store the most recent dates with corresponding origine (i.e., ticker):
dataframeObjects <- names(which(unlist(eapply(.GlobalEnv, is.data.frame))))
# Tentative 1
for (i in seq_along(dataframeObjects)) {
mostRecentDates <- max(dataframeObjects[i]$Date)
}
# Tentative 2
for (i in 1:length(dataframeObjects)) {
mostRecentDates <- max(dataframeObjects[i]["Date"])
}
Both tentatives give a [1]NA
when invoking variable mostRecentDates
.
Important: In the final code there won't be any tickers
and tickers2
variables. There will be a certain quantity of data.frame
objects that will be loaded locally and it is those that will be searched for the last date available.
My question:
- What code is needed in order to store the most recent dates of all
data.frame
objects (if possible by invokingdataframeObjects
, but nottickers
andtickers2
)?
Thanks in advance.
Systems used:
- R version: 4.1.1 (2021-08-10)
- RStudio version: 1.4.1717
- OS: macOS Catalina version 10.15.7 and macOS Big Sur version 11.6