Importare e gestire dati finanziari in R
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
"2017-01-01" (inizio)"2017-03-31" (fine)"2017-02-01" (metà)DGS10GDP# Aggrega a trimestrale QGS10 <- apply.quarterly(DGS10, median, na.rm = TRUE)# Unisci l’aggregato trimestrale con il PIL trimestrale QGS10_GDP <- merge(QGS10, GDP) QGS10_GDP
DGS10 GDP
2015-01-01 NA 17783.6
2015-03-31 1.97 NA
2015-04-01 NA 17998.3
2015-06-30 2.19 NA
2015-07-01 NA 18141.9
2015-09-30 2.20 NA
2015-10-01 NA 18222.8
2015-12-31 2.23 NA
yearmon() per dati mensiliyearqtr() per dati trimestralias.Date("2017-01-01")
"2017-01-01"
as.yearmon("2017-01-01")
"Jan 2017"
as.yearqtr("2017-01-01")
"2017 Q1"
# Converti entrambi gli indici in yearqtr
index(QGS10) <- as.yearqtr(index(QGS10))
index(GDP) <- as.yearqtr(index(GDP))
# L'unione "funziona e basta"
merge(QGS10, GDP)
DGS10 GDP
2015 Q1 1.97 17783.6
2015 Q2 2.19 17998.3
2015 Q3 2.20 18141.9
2015 Q4 2.23 18222.8
# Ultima osservazione propagata all’indietro
QGS10_GDP_locb <- na.locf(QGS10_GDP, fromLast = TRUE)
# Sottoinsieme per indice di inizio periodo
QGS10_GDP_first_period <- QGS10_GDP_locb[index(GDP)]
QGS10_GDP_first_period
DGS10 GDP
2015-01-01 1.97 17783.6
2015-04-01 2.19 17998.3
2015-07-01 2.20 18141.9
2015-10-01 2.23 18222.8
Importare e gestire dati finanziari in R