R Load CSV

How to Import CSV Files Into R

By Daniel D. Bonneau in R Basics

June 22, 2022


Load CSV File in R

Here, we will use two simple ways to read csv data into our RStudio. First, we will cover the “base r” way using read.csv(), which doesn’t require loading any additional packages. Then, we will import a csv into RStudio using the read_csv() function from the readr package.

Setting Our Working Directory

Before we get started, we need to set our working directory. This points R to the correct place to look for your file. Your csv lives somewhere on your computer, but R doesn’t magically know where that file is. So, by setting our working directory, we’re telling R - ‘hey, I’ve got this data set, and it’s in this folder, so let’s look in there’.

setwd("C:/Users/danbo/Documents/r_website/content/blog/read_csv_in_r")

That is where my data set lives. For you, it will obviously be different. If you’re not sure how to find the folder structure that leads you to your file, you can follow these quick steps:

  1. Find your file in the file explorer
  2. Right-click on the file and find the option called “Properties”
  3. Copy the ‘Location’ portion of the menu that pops up

Note: The above instructions are specifically for those on Windows systems. However, following a similar process on a Mac should get you to the same point.

Now that you have the file path, you can copy and paste that directly as shown in the code above. Make sure any \ from your copy of the location are changed to /, though.

Import CSV to R (Using Base R)

Now that we have set our working directory and told R where to find our data set. We can finally read our csv file into our R environment so we can clean, analyze, and visualize our data. To do this in base R, we will use the read.csv() function. The only thing we need to pass into this function to read in our data is the name of the file, wrapped in quotations. For example:

base_r <- read.csv("simple_revenue.csv")

That’s it! Our csv file is now loaded and ready to be used within R. Notice a few things from the above code snippet: We use <- called the ‘assignment operator’ to assign the csv file we’re reading in to a variable in R. I decided to call mine base_r, but you are free to call it whatever you’d like (except data, don’t call it data).

To verify that we have our data set in R, we can run the head() function. This function allows us to see only the first six rows of our data set and serves as an excellent way to make sure your data set was read in properly. If you want to see the whole data set, just replace head() in the code below with View() (note the capital ‘V’).

head(base_r)
##       date    revenue
## 1 6/1/2022   $307.00 
## 2 6/2/2022   $557.00 
## 3 6/3/2022   $549.00 
## 4 6/4/2022 $1,159.00 
## 5 6/5/2022 $1,525.00 
## 6 6/6/2022 $1,310.00

Here, we see the first six rows of our data with for both the date and revenue columns. There are a few more checks we’d want to do to make sure our data was read in with the correct types, but that involves cleaning it a little bit and is beyond the scope of this article.

Now, we could stop there and we’d be able to read in pretty much any csv file we could want. However, by using the readr package, we can read in our csv files much more quickly, which can be useful for data sets with thousands of rows.

read_csv in R

To start, this method requires loading in the readr package. To read in a package we have previously installed, we use the library() function, passing the name of the package into the parentheses. If you haven’t installed the package before, you would do: install.packages("readr") and then the code that follows. If you’ve installed the package before, you can just use the code below to load it into your working environment.

Within this package, we’re going to use the read_csv() function in the same way we used the read.csv() function above.

library(readr)

readr_r <- read_csv("simple_revenue.csv")

head(readr_r)
## # A tibble: 6 × 2
##   date     revenue  
##   <chr>    <chr>    
## 1 6/1/2022 $307.00  
## 2 6/2/2022 $557.00  
## 3 6/3/2022 $549.00  
## 4 6/4/2022 $1,159.00
## 5 6/5/2022 $1,525.00
## 6 6/6/2022 $1,310.00

As you can see from our use of head() on this data set, both of our columns are character strings. That’s obviously not how we want the data to be - we’d want the date column to be a date type, and the revenue column to be a number.

That cleaning will be shown in a later article.

Closing Thoughts

I hope this tutorial on how to read a csv file into R was helpful. If there is anything in particular you’d like to see me cover, feel free to contact me and let me know what tutorials you’d like to see next.

’Til then - Happy R’ing!