Importación y gestión de datos financieros en R
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
"2017-01-01"(primero)"2017-03-31" (último)"2017-02-01" (medio)DGS10GDP# Agrega a trimestral QGS10 <- apply.quarterly(DGS10, median, na.rm = TRUE)# Fusiona el agregado trimestral con el PIB trimestral 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() para datos mensualesyearqtr() para datos trimestralesas.Date("2017-01-01")
"2017-01-01"
as.yearmon("2017-01-01")
"Jan 2017"
as.yearqtr("2017-01-01")
"2017 Q1"
# Convert both indexes to yearqtr
index(QGS10) <- as.yearqtr(index(QGS10))
index(GDP) <- as.yearqtr(index(GDP))
# La fusión "simplemente funciona"
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
# Última observación propagada hacia atrás
QGS10_GDP_locb <- na.locf(QGS10_GDP, fromLast = TRUE)
# Subconjunto por índice de inicio de 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
Importación y gestión de datos financieros en R