In this tutorial, we will show you one of the easiest methods you can use to import CSV files into R.
We will be working with RStudio, a program that makes it easier for users to work with R, and we will be using the the readr package.
Quick Steps
- Type install.packages(“tidyverse”) and click enter on your keyboard to install the tidyverse packages
- From the Environment tab of RStudio, click Import Dataset -> From Text (readr)…
- Click Browse
- Browse to the CSV file that you want to import
- Double-click on this file
- Click Import
Install Readr
The readr package makes it easy to import CSV files into R, but R doesn’t come with this package installed. If you haven’t already installed readr, we recommend installing the complete set of tidyverse packages, which includes readr.
To do this, type the following in your RStudio console:
install.packages(“tidyverse”)
Select the enter key on your keyboard. You may need to wait for a few minutes while the packages are installed.
Import Your CSV File into R
First, ensure that the Environment tab is selected in the top right panel of RStudio.
Click Import Dataset and select From Text (readr) … from the drop-down menu as illustrated in the screenshot below:
Note that the FromText (base)… option may also be used to import CSV files. However we recommend readr, especially if you are importing a large data set.
You will now see the the Import Text Data window illustrated below.
Click the Browse button.
Browse to the CSV file that you wish to import into R and double-click on it. Your Import Text Data window should now look similar to the one below:
In the screenshot above we see:
(1) The file and path (or URL) of the CSV file we want to import into R.
(2) The Data Preview of this file.
(3) The auto-generated name that the file will have in R. We can change this name by placing our cursor in the Name field and editing it.
(4) Ensure that the First Row as Names box is checked if the first row of your data set contains your variable names.
(5) The code for importing the CSV file into R
(6) The Import button.
Click Import. In RStudio, you should now see something similar to the screenshot below:
In our screenshot above, the following are highlighted:
(1) A view of the first records of our imported data set. You can use the scrollbar to the right to see the rest of the data.
(2) Information about this data set. A spec_tbl_dbf is a type of tibble data frame, Our tibble comprises 30 observations of three variables.
(3) The R code used to import our CSV file.
Reviewing Your Imported Data Set
After you have imported your CSV file into R, it’s a good idea to review it. One way to do this is to check the data view in the top left window of RStudio as illustrated in our last screenshot (see note (1)).
To get additional information, you may also wish to display the first part of your data set by typing the following:
head (your.data)
Replace your.data here with the name of your data frame in R. Our data frame is called sats_exam_scores, so we type:
head(sats_exam_scores)
Then select enter on your keyboard. By default, the head() command displays the first six records of your data frame, so you should see something like the following:
We can see that all three of our variable are of the dbl type. That is, they are stored as numeric data.
Save Your Tibble Data Frame (Optional)
If you want to save your tibble data frame so that you can work with it in future R sessions, you can do this as follows:
saveRDS(Rtibble, “savedtibble.rds”)
Replace Rtibble with the name of your tibble data frame in R. The name of ours is: sats_exam_scores.
Replace savedtibble.rds with the file name and file path with which you want to use to save your file on your computer. There are a couple of points to highlight here:
- If you don’t specify a file path, your tibble data frame will be saved to your “working directory”.
- File paths need to be typed in a particular way in R. Specifically, we must replace the backwards slashes (\) that we normally see in Windows file paths with forward slashes. I want to save my tibble data frame to the file path C:\Users\user\OneDrive\Documents\R\Datasets with the file name sats_exam_scores.rds so in R, this will be C:/Users/user/OneDrive/Documents/R/Datasets/sats_exam_scores.rds.
For our example, the command we will type in RStudio is as follows:
saveRDS(sats_exam_scores, “C:/Users/user/OneDrive/Documents/R/Datasets/sats_exam_scores.rds”)
Select enter on your keyboard to save the tibble data frame to the specified location on your computer.
***************
That’s it for this tutorial. You should now be able to import CSV files into R using RStudio and the readr package.
***************