In this article we will work on importing .csv files into R from different sources using read.csv() command.


Theory

.csv stands for comma separated values and as a format it represents a plain text file containing a list of data with a “,” or “;” separator.

It is one of the most popular formats you will come across when getting raw data for your data projects and is widely used across various platforms and programming languages.

Okay, enough theory, let’s get to practice!



Application

Below are the steps we are going to take to make sure we do master the skill of importing .csv files into R:

  1. Basic read.csv() command description
  2. Importing a .csv file from an URL
  3. Importing a .csv file from your computer



Part 1. Basic read.csv() command description

The very brief theoretical explanation of the function is the following:

read.csv(file, header = TRUE, sep = ",")

“file” part of the function requires you to enter the directory of the .csv file you are importing.

“header” part of the function can take two values: TRUE and FALSE.

1. In case of selecting “TRUE”, the function will take the first row and the names for the columns (most common case).

2. In case of selecting “FALSE”, the first row of the .csv file will be treated as observations.

“sep” part of the function allows you to identify and set the separator between the values in the .csv file. Most commonly it will be “,” which then leads to the function value having sep = “,”.

If you would like to learn the full description of the command, you can read about it here.



Part 2. Importing a .csv file from an URL

In order to import a .csv file from a website (or from an URL), we need the exact link address of the destination file.

Where to find it? Good question!

Let’s take a look at the following example:

1. Take a look at a list of various datasets here.

2. Choose a dataset that interests you (I choose the one called “TitanicSurvical”).

3. On the right hand of the screen they have a link to the .csv file where you see “CSV“. Right click on it and copy the link address.

4. Use this link in the file path in the read.csv() command to import the file. Don’t forget that you need to define a variable into which you will be importing the dataset (I called mine “mydata”).


mydata<-read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/carData/TitanicSurvival.csv", header=TRUE)

Note: unfortunately the entire code didn't fit in one line, but as you copy and paste it into your R console, it will appear as one line.



Part 3. Importing a .csv file from the computer

In order to import a .csv file from the computer, we need the exact location of the destination file.

So how do we do that? Let's find out!

First of all, we need to know where the file is stored on your computer.

In my case, I use Mac OS and my file is stored on my desktop. In order to find the location of the file on Mac OS you can right click on the file and choose "Get Info"; on Windows you can right click on the file and choose "Properties".

In my case, the location of the file in R format is: /Users/DataSharkie/Desktop/TitanicSurvival.csv

Use this local path in the file path in the read.csv() command to import the file. Don't forget that you need to define a variable into which you will be importing the dataset (I called mine "mydata").


mydata<-read.csv("/Users/DataSharkie/Desktop/TitanicSurvival.csv", header=TRUE)



If you are interested to learn more about importing different data formats into R, you can find more articles here.