In this article, we will show you how to read CSV file in R. When we work with R, there are many situations that we need to read or import data from a file like CSV. After cleaning, filtering and processing data, we may need to export the data back to CSV file. With this article, we hope to facilitate you in those kinds of tasks.

1. Read CSV File in R

To read CSV file in R, we can use R base functions and don’t need to install additional packages.

1.1. Read CSV file in R using read.csv() function

The general syntax is:

– To read CSV file that has header row and its fields are separated by comma

– To read CSV file that has the header row and its fields are separated by the comma. The same with example 1 but shorter by using default parameters of the method.

Note that the default parameters: header = True, sep = “,”.

– To read CSV file that doesn’t have header row and its fields are separated by tab

1.2. Read CSV file in R using read.csv2() function

We can use the function read.csv2() to read CSV file in R. The function read.csv2() is almost identical with the read.csv() except the default parameters. Let’s see its syntax

The function has default parameters: sep=”;” and dec = “,”. So, it’s suitable for countries that use comma as decimal point and a semicolon as field separator

– To read CSV file that has header row and its fields are separated by semicolon

Of course, we can use read.csv2() function to read the CSV file that has fields are separated by the comma.

1.3. Read CSV file in R using read.table() function

We can use the function read.table() to read the CSV file.

– To read CSV file that has header row and its fields are separated by comma

– Read CSV file that doesn’t have header row and its fields are separated by tab

1.4. Read CSV file in R using fread() function

We can use the fread(), a function a provided by the data.table package, to read the CSV file in R. It is Similar to read.table but faster and more convenient. All controls such as sep (field separator), header and nrows (number of rows are automatically detected.

We will get through some examples by using the fread() function to read CSV file in R as below:

–  To read CSV file that has a header row and its fields are separated by the comma:

Note that the header, field separator (in this case is the comma), will be automatically detected.

–  To read CSV file that doesn’t have header row and its fields are separated by tab

2. Summary

We have learned to read CSV file in R by using R base functions.  The read.csv(), read.csv2(), read.table() and fread() functions can be used to read CSV file. Note that read.table() is the generic method that can be used to read more different types of text files. After reading data into a data frame, you may need to manipulate that data. In that case, you can refer to the following articles:

R – How To Order A Data Frame

R Data Frame and basic functions

R – Rename Column of Data Frame

R – Add New Column To A Data Frame

Write CSV File in R

 

 

0 0 vote
Article Rating