CSV files

What a csv file looks like

file name ends with a .csv

What a csv file looks like on the inside

file name ends with a .csv

What an Excel file looks like

file name ends with a .xls or .xlsx

What an Excel file looks like on the inside

file name ends with a .xls or .xlsx

Importing CSV files

  • Importing CSV is part of base R, no package needed
  • But we’re going to use a package anyway, readr

Two ways to get data

  • If you have the URL address
  • If you have the file on your computer

Get the URL

Right click the link of the data and click Copy Link Address

read.csv()

The function is read.csv() and put the URL address in “” and add the stringsAsFactors=F

df_csv <- read.csv("https://data.ct.gov/api/views/iyru-82zq/rows.csv?accessType=DOWNLOAD", stringsAsFactors=F)

stringsAsFactors=F

Why?

Blame statisticians.

Back when R was created the users weren’t using it as we use it now, with all these different strings.

The other way to import the data: Download it

When you right click on the link, instead of clicking Copy Link Address– this time, click Save Link As…

Save to the directory you’re working in

After saving to the directory, click on the circle arrow on the right to refresh the files to make sure it’s there.

Note: How to change directories in RStudio

Either by typing setwd("/directory/where/you/want") or by clicking in the menu up top Session > Set Working Directory > Choose Directory…

Importing local csv data

Just like before, except instead of the URL, it’s the name of the file.

Note: This will only work if the working directory is set to where the csv file is.

df_csv <- read.csv("data/Admissions_to_DMHAS_Addiction_Treatment_by_Town__Year__and_Month.csv", stringsAsFactors=F)