R에서 금융 데이터 가져오기와 관리
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
"2017-01-01"(시작)"2017-03-31" (끝)"2017-02-01" (중간)DGS10GDP# 분기로 집계 QGS10 <- apply.quarterly(DGS10, median, na.rm = TRUE)# 분기 집계와 분기 GDP 병합 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()yearqtr()as.Date("2017-01-01")
"2017-01-01"
as.yearmon("2017-01-01")
"Jan 2017"
as.yearqtr("2017-01-01")
"2017 Q1"
# 두 인덱스를 yearqtr로 변환
index(QGS10) <- as.yearqtr(index(QGS10))
index(GDP) <- as.yearqtr(index(GDP))
# 병합은 그대로 동작
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
# 마지막 관측치를 역방향으로 채우기
QGS10_GDP_locb <- na.locf(QGS10_GDP, fromLast = TRUE)
# 기간 시작 인덱스로 서브셋
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
R에서 금융 데이터 가져오기와 관리