This file is part of High way crash project and contains all the code for downloading and converting. If you have any question, please email at jhli.potomac@gmail.com. Thanks
[data source] (ftp://ftp.nhtsa.dot.gov/FARS/2014/DBF/)
#load Rcurl for unzipping files
library(RCurl)
## Loading required package: bitops
#load the foreign for reading dbf files
library(foreign)
#load plyr for rbind.fill
library(plyr)
download and unzip the files
#set current directory
setwd("C:/Users/Jianhua/HighWayCrash_Project/data")
#set parameters for downloading 2010-2014 five years data
url <- "ftp://ftp.nhtsa.dot.gov/FARS"
years = c('2010', '2011','2012', '2013', '2014')
#loop through the years
lapply(years, function(x){
url <- paste(url,'/', x, '/DBF/FARS',x, '.zip', sep = '')
filename<-paste ('FARS',x,'.zip', sep='')
foldername<-paste('FARS',x, sep='')
filename
#download the files
if (file.exists(filename)==FALSE){
download.file(url, filename)
}
#create the corresponding folders
if (file.exists(foldername)==FALSE){
dir.create(foldername)
}
#unzip files and remove the zipped files
#list.files(path = foldername, pattern='*.dbf')
if (length(list.files(path = foldername, pattern='*.dbf')) == 0){
unzip(filename)
}
#move unzipped files into corresponding folders,
for (fl in (list.files(pattern="*.dbf"))){
file.copy(fl,foldername)
#remove the unzipped files
file.remove(fl)
}
# 2009 file ended with "DBF"
for (fl in (list.files(pattern="*.DBF"))){
file.copy(fl,foldername)
#remove the unzipped files
file.remove(fl)
}
})
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
##
## [[4]]
## NULL
##
## [[5]]
## NULL
Convert all dbf files into csv files
#rm(list = ls())
directory <- getwd()
#subdirectory = paste(directory,'FARS2014', sep='/' )
#fileNames = list.files(subdirectory, pattern = "*.dbf",full.names = FALSE)
fileNames = c("accident","cevent", "person")
lapply(fileNames, function(x){
file_fullNames <- list.files(path = directory,
recursive = TRUE,
pattern = x,
full.names= TRUE)
dat <- do.call(rbind.fill, lapply(file_fullNames, read.dbf))
##export the file as csv files
write.csv(dat,paste(x, '.csv', sep=''))
}
)
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL