-1

I have a dataset from of VINs registered in NYS. I've read this file into R and would like to use the NHTSA VIN API.

Let's say the file looks like this:

VIN = c("5XYPKDA55KG446393", "5XYPHDA56KG443792", "5XYPGDA33KG432573") 
myear4 = c("2019", "2019", "2019") 
ZIP = = c("10562", "10923", "13642") 

df = data.frame(VIN,myear4,ZIP) 

How do I have R:

  1. create a API call for each row

https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPKDA55KG446393?format=csv&modelyear=2019

https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPHDA56KG443792?format=csv&modelyear=2019

https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPGDA33KG432573?format=csv&modelyear=2019

  1. combine the results together match
  • 1
    Have you tried anything? You could probably do something like `x<-lapply(sprintf("https://.../%s?format=csv&modelYear=%d",df$VIN,df$myear4), httr::GET)` then `do.call(rbind,lapply(x,read.csv,stringsAsFactors=F))`. – r2evans May 06 '18 at 00:21
  • @hrbrmstr What code did I use to scape the original VIN data? None. The publisher offers the data in CSV format. I am just using reader to bring the file into R. – Josh De La Rosa May 06 '18 at 01:33
  • @r2evans I tried `x<-lapply(sprintf("vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/…), httr::GET) do.call(rbind,lapply(x,read.csv,stringsAsFactors=F))` and received the error message `do.call(rbind,lapply(x,read.csv,stringsAsFactors=F)) Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection` Sorry for the newbee question but I haven't found the answer to on SO yet – Josh De La Rosa May 06 '18 at 01:44

1 Answers1

-1

I've had good luck with the downloader pkg. It's a light wrapper around the base R downloading functions, Just replace the parts of those URL that will change. Doesn't look like you are using the ZIP:

library(downloader)
for( i in 1:3){ 
  download.file( paste0("https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/",
                        VIN[i],"?format=csv&modelyear=",myear4[i]), 
                destfile=paste0("test_", i ,".csv"))}
#-------------------
trying URL 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPKDA55KG446393?format=csv&modelyear=2019'
Content type 'application/octet-stream' length 2352 bytes
==================================================
downloaded 2352 bytes

trying URL 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPHDA56KG443792?format=csv&modelyear=2019'
Content type 'application/octet-stream' length 2351 bytes
==================================================
downloaded 2351 bytes

trying URL 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/5XYPGDA33KG432573?format=csv&modelyear=2019'
Content type 'application/octet-stream' length 2349 bytes
==================================================
downloaded 2349 bytes

I'm not sure if the files would be guaranteed to have all the same columns but if they were you could read them into R with read.csv and `do.call(rbind, ...) them together. There are lots of asked and answered questions about that part and multi-part questions are discouraged on SO, so I'm not thinking I should continue.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • sorry for the newbee questions. Yes, all fo the files have the same columns. What should I search for in SO to find the answer to the bind question? – Josh De La Rosa May 06 '18 at 01:47
  • Flipping A! I got a downvote for this??? The I think your question should start with what you’ve tried so far. – IRTFM May 06 '18 at 01:52
  • Thanks again @42- This worked: `files <- list.files(pattern = '\\.csv') tables <- lapply(files, read.csv, header = TRUE) combined.df <- do.call(rbind , tables)` – Josh De La Rosa May 06 '18 at 02:15