Let’s first download all the files from the DOT website. Different years have different URL formats as well as file formats. As a result we have to account for these variations in the download code below. The files are large so this step takes some time.
# Download files for years 2010 to 2013
for (p in 10:13) {
# The place where to find the file
fileURL <- paste("http://www.fhwa.dot.gov/bridge/nbi/20", as.character(p),
"/delimited/NE", as.character(p), ".tab", sep = "")
# download the file to a local directory
fileName <- paste("nebridges20", as.character(p), ".tab", sep = "")
download.file(fileURL, destfile = fileName, method = "auto")
}
# Download files for years 2000 to 2009
for (p in 0:9) {
# The place where to find the file
fileURL <- paste("http://www.fhwa.dot.gov/bridge/nbi/200", as.character(p),
"/NE0", as.character(p), ".tab", sep = "")
# download the file to a local directory
fileName <- paste("nebridges200", as.character(p), ".tab", sep = "")
download.file(fileURL, destfile = fileName, method = "auto")
}
# Download files for years 1992 to 1999
for (p in 92:99) {
# The place where to find the file
fileURL <- paste("http://www.fhwa.dot.gov/bridge/nbi/19", as.character(p),
"/NE", as.character(p), ".tab", sep = "")
# download the file to a local directory
fileName <- paste("nebridges19", as.character(p), ".tab", sep = "")
download.file(fileURL, destfile = fileName, method = "auto")
}
Let us record the date when the data was downloaded
# date of download
dateDownloaded <- date()
date()
## [1] "Wed Jan 7 08:03:18 2015"
# read the file into a data.frame
Let us visualize the more recent bridge condition from 2010 to 2013 first.
Legend: 1-4 (RED-bad); 5-7 (YELLOW-ok); 8-9,blank or N (GREEN-good)
# For 2010 to 2013
for (p in 10:13) {
# The place where to find the file
fileName <- paste("nebridges20", as.character(p), ".tab", sep = "")
bridgedata <- read.table(fileName, sep = ",", header = TRUE, fill = TRUE,
quote = "")
# normalize thelongitude
DF <- data.frame(bridgedata$LONG_017)
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(DF$bridgedata.LONG_017, function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lon <- 0 - (result[, 1] + result[, 2]/60 + result[, 3]/3600)
# normalize the latitude
DF <- data.frame(bridgedata$LAT_016)
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(DF$bridgedata.LAT_016, function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lat <- (result[, 1] + result[, 2]/60 + result[, 3]/3600)
### Map bridge locations
library("maps")
# prepare the nebraska map
map("county", region = "nebraska", plot = TRUE, fill = FALSE, col = palette())
temp <- paste("Bridge Health 20", as.character(p), sep = "")
title(temp)
legendtext <- c("1-4", "5-7", "8,9,blank,N")
colors = c("RED", "YELLOW", "GREEN")
legend("bottomleft", legendtext, fill = colors)
# plot the bridge locations
bridgehealth <- data.frame(cbind(lon, lat))
bridgehealth["health"] <- NA
bridgehealth$health <- bridgedata$DECK_COND_058
head(bridgehealth)
colorBridgeHealth <- function(a) {
for (i in 1:length(a[, 3])) {
if ((a[, 3][i] == "1" || a[, 3][i] == "2" || a[, 3][i] == "3" ||
a[, 3][i] == "4")) {
points(y = a[, 2][i], x = a[, 1][i], col = "red", pch = 20,
cex = 0.25)
} else if ((a[, 3][i] == "5" || a[, 3][i] == "6" || a[, 3][i] == "7")) {
points(y = a[, 2][i], x = a[, 1][i], col = "yellow", pch = 20,
cex = 0.25)
} else if (a[, 3][i] == " " || (a[, 3][i] == "8" || a[, 3][i] == "9" ||
a[, 3][i] == "N")) {
points(y = a[, 2][i], x = a[, 1][i], col = "green", pch = 20,
cex = 0.25)
}
}
}
colorBridgeHealth(bridgehealth)
}
For 2013 which is the last set of data we have in the loop above, let us look at the years in which the bridges were built
# Frequency of bridges built in various years
count <- table(bridgedata$YEAR_BUILT_027)
plot(count)
title("# of Bridges built over the years")
title(xlab = "years")
Let us also examine the frequency with which the bridges are examined
# Frequency of inspecting the bridges
count <- table(bridgedata$INSPECT_FREQ_MONTHS_091)
plot(count)
title("Frequency of inspecting the bridges")
title(xlab = "months")
We can see how the inspection frequency changes with respect to the superstructure and the deck condition
# Distribution of Inspection requency over the deck and superstructure
# condition
histogram(~INSPECT_FREQ_MONTHS_091 | bridgedata$SUPERSTRUCTURE_COND_059, bridgedata)
histogram(~INSPECT_FREQ_MONTHS_091 | bridgedata$DECK_COND_058, bridgedata)
# for 1992 to 2009
library("stringr")
library("maps")
for (p in 1992:2009) {
# The place where to find the file
fileName <- paste("nebridges", as.character(p), ".tab", sep = "")
# These files have to read one LINE at a time
bridgedata <- readLines(fileName, n = -1, warn = TRUE, encoding = "ascii",
skipNul = FALSE)
# Extract data from the long strings based on position and enter it into a
# data frame
bridgeDF = data.frame(matrix(vector(), length(bridgedata), 5, dimnames = list(c(),
c("ID", "Year", "lat", "lon", "deckHealth"))), stringsAsFactors = F)
for (i in 1:length(bridgedata)) {
bridgeDF$ID[i] <- str_sub(bridgedata[i], 4, 18)
bridgeDF$Year[i] <- str_sub(bridgedata[i], 157, 160)
bridgeDF$lat[i] <- str_sub(bridgedata[i], 130, 137)
bridgeDF$lon[i] <- str_sub(bridgedata[i], 138, 146)
bridgeDF$deckHealth[i] <- str_sub(bridgedata[i], 259, 259)
}
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(as.numeric(bridgeDF$lon), function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lon <- 0 - (result[, 1] + result[, 2]/60 + result[, 3]/3600)
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(as.numeric(bridgeDF$lat), function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lat <- (result[, 1] + result[, 2]/60 + result[, 3]/3600)
### Map bridge locations prepare the nebraska map
map("county", region = "nebraska", plot = TRUE, fill = FALSE, col = palette())
temp <- paste("Bridge Health ", as.character(p), sep = "")
title(temp)
legendtext <- c("1-4", "5-7", "8,9,blank,N")
colors = c("RED", "YELLOW", "GREEN")
legend("bottomleft", legendtext, fill = colors)
# plot the bridge locations
bridgehealth <- data.frame(cbind(lon, lat))
bridgehealth["health"] <- NA
bridgehealth$health <- bridgeDF$deckHealth
head(bridgehealth)
colorBridgeHealth <- function(a) {
for (i in 1:length(a[, 3])) {
if ((a[, 3][i] == "1" || a[, 3][i] == "2" || a[, 3][i] == "3" ||
a[, 3][i] == "4")) {
points(y = a[, 2][i], x = a[, 1][i], col = "red", pch = 20,
cex = 0.25)
} else if ((a[, 3][i] == "5" || a[, 3][i] == "6" || a[, 3][i] == "7")) {
points(y = a[, 2][i], x = a[, 1][i], col = "yellow", pch = 20,
cex = 0.25)
} else if (a[, 3][i] == " " || (a[, 3][i] == "8" || a[, 3][i] == "9" ||
a[, 3][i] == "N")) {
points(y = a[, 2][i], x = a[, 1][i], col = "green", pch = 20,
cex = 0.25)
}
}
}
colorBridgeHealth(bridgehealth)
}
# For 2010 to 2013
for (p in 10:13) {
# The place where to find the file
fileName <- paste("nebridges20", as.character(p), ".tab", sep = "")
# These files can be read as a table
bridgedata <- read.table(fileName, sep = ",", header = TRUE, fill = TRUE,
quote = "")
# normalize thelongitude
DF <- data.frame(bridgedata$LONG_017)
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(DF$bridgedata.LONG_017, function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lon <- 0 - (result[, 1] + result[, 2]/60 + result[, 3]/3600)
# normalize the latitude
DF <- data.frame(bridgedata$LAT_016)
# decimal degrees = degrees + minutes/60 + seconds/3600
result <- t(sapply(DF$bridgedata.LAT_016, function(y) {
c(y%/%1e+06, (y - y%/%1e+06 * 1e+06)%/%10000, (y - y%/%10000 * 10000)/100)
}))
lat <- (result[, 1] + result[, 2]/60 + result[, 3]/3600)
### Map bridge locations
library("maps")
# prepare the nebraska map
map("county", region = "nebraska", plot = TRUE, fill = FALSE, col = palette())
temp <- paste("Bridge Health 20", as.character(p), sep = "")
title(temp)
legendtext <- c("1-4", "5-7", "8,9,blank,N")
colors = c("RED", "YELLOW", "GREEN")
legend("bottomleft", legendtext, fill = colors)
# plot the bridge locations
bridgehealth <- data.frame(cbind(lon, lat))
bridgehealth["health"] <- NA
bridgehealth$health <- bridgedata$DECK_COND_058
head(bridgehealth)
colorBridgeHealth <- function(a) {
for (i in 1:length(a[, 3])) {
if ((a[, 3][i] == "1" || a[, 3][i] == "2" || a[, 3][i] == "3" ||
a[, 3][i] == "4")) {
points(y = a[, 2][i], x = a[, 1][i], col = "red", pch = 20,
cex = 0.25)
} else if ((a[, 3][i] == "5" || a[, 3][i] == "6" || a[, 3][i] == "7")) {
points(y = a[, 2][i], x = a[, 1][i], col = "yellow", pch = 20,
cex = 0.25)
} else if (a[, 3][i] == " " || (a[, 3][i] == "8" || a[, 3][i] == "9" ||
a[, 3][i] == "N")) {
points(y = a[, 2][i], x = a[, 1][i], col = "green", pch = 20,
cex = 0.25)
}
}
}
colorBridgeHealth(bridgehealth)
}
A few observations regarding the health data:
1. There is a significant increase in the number of data points between 1998 and 1999
2. A lot of “bad” bridges tend to be in the lower right hand side counties and the top right set of counties.
All the data here used here can be downloaded in its raw format from [here.] (http://www.fhwa.dot.gov/bridge/nbi/ascii.cfm)