Typically, this is the commonly advised way in R to download the 10-year federal note yield
library(quantmod)
getSymbols("DGS10",src='FRED')
Fed<-last(DGS10)
The problem is that the value retrieved is one day older than what is available on the treasury website https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield
Is there a way to parse the above webpage to retrieve the most recent 10 yr treasury note yield?
rvest
is a good option, as these are rendered tables. Another is:
library(httr)
library(XML)
year = 2016
URL = paste0("https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldYear&year=",year)
urldata <- httr::GET(URL)
data <- XML::readHTMLTable(rawToChar(urldata$content),
stringsAsFactors = FALSE)
# Finds html based list element w/daily data for the year
namesCheck <- c("Date","1 mo","3 mo","6 mo","1 yr","2 yr","3 yr","5 yr","7 yr","10 yr","20 yr","30 yr")
dataCheck <- NULL
for(i in 1:length(data)){
dataCheck[[i]] <- names(data[[i]])
}
## Returns appropriate frame
dataCheck <- which(unlist(lapply(1:length(dataCheck), function(i)
(dataCheck[[i]] == namesCheck)[1])) == TRUE)
data <- as.data.frame((data[dataCheck]))
names(data) <- gsub("NULL.","", names(data)) # Take out "NULL."
tail(data)
# Date 1.mo 3.mo 6.mo 1.yr 2.yr 3.yr 5.yr 7.yr 10.yr 20.yr 30.yr
# 245 12/22/16 0.42 0.51 0.65 0.87 1.22 1.54 2.04 2.36 2.55 2.86 3.12
# 246 12/23/16 0.42 0.52 0.65 0.87 1.22 1.54 2.04 2.35 2.55 2.86 3.12
# 247 12/27/16 0.50 0.51 0.66 0.89 1.28 1.58 2.07 2.37 2.57 2.88 3.14
# 248 12/28/16 0.48 0.53 0.62 0.90 1.26 1.55 2.02 2.32 2.51 2.83 3.09
# 249 12/29/16 0.39 0.47 0.62 0.85 1.22 1.49 1.96 2.30 2.49 2.82 3.08
# 250 12/30/16 0.44 0.51 0.62 0.85 1.20 1.47 1.93 2.25 2.45 2.79 3.06