R: Getting and Importing Data

R can directly import data from URL’s by using the following syntax:

fileCameras <- "https://data.baltimorecity.gov/api/views/hdyb-27ak/rows.csv?accessType=DOWNLOAD"
download.file(fileCameras, destfile="baltimore_cameras.csv",method="curl")

The first assignment sets the variable fileCameras to the URL for the csv. How I got that URL was by going to the public dataset for the Baltimore city database. I went to CCTV cameras and right clicked the CSV option on the export tab. I copied the URL for the CSV file and pasted it in as the value for fileCameras.

After that I made use of the R command download, and it's method .file. The .file method takes three params: The file path (URL), the destination on your hard drive for saving it and the method. I've been told that the method is optional on a PC, but on MAC curl is required as it's value.

Once you do these commands the CSV will download to your local directory that you are in, in R.

Leave a Reply

Your email address will not be published. Required fields are marked *