-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 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
  • 251,731
  • 20
  • 347
  • 472