15.1 C
London
Friday, July 5, 2024
HomeRImport & Export Data in RHow to Import CSV Files into R (Step-by-Step)

How to Import CSV Files into R (Step-by-Step)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Suppose I have a CSV file called data.csv saved in the following location:

C:UsersBobDesktopdata.csv

And suppose the CSV file contains the following data:

team, points, assists
'A', 78, 12
'B', 85, 20
'C', 93, 23
'D', 90, 8
'E', 91, 14

There are three common ways to import this CSV file into R:

1. Use read.csv from base R (Slowest method, but works fine for smaller datasets)

data1 C:\Users\Bob\Desktop\data.csv", header=TRUE, stringsAsFactors=FALSE)

2. Use read_csv from readr package (2-3x faster than read.csv)

library(readr)

data2 C:\Users\Bob\Desktop\data.csv")

3. Use fread from data.table package (2-3x faster than read_csv)

library(data.table)

data3 C:\Users\Bob\Desktop\data.csv")

This tutorial shows an example of how to use each of these methods to import the CSV file into R.

Method 1: Using read.csv

If your CSV file is reasonably small, you can just use the read.csv function from Base R to import it.

When using this method, be sure to specify stringsAsFactors=FALSE so that R doesn’t convert character or categorical variables into factors.

The following code shows how to use read.csv to import this CSV file into R:

#import data
data1 C:\Users\Bob\Desktop\data.csv", header=TRUE, stringsAsFactors=FALSE)

#view structure of data
str(data1)

'data.frame':   5 obs. of  3 variables:
 $ team   : chr  "'A'" "'B'" "'C'" "'D'" ...
 $ points : int  78 85 93 90 91
 $ assists: int  12 20 23 8 14

Method 2: Using read_csv

If you’re working with larger files, you can use the read_csv function from the readr package:

library(readr)

#import data
data2 C:\Users\Bob\Desktop\data.csv")

#view structure of data
str(data2)

'data.frame': 5 obs. of 3 variables:
 $ team : chr "'A'" "'B'" "'C'" "'D'" ...
 $ points : int 78 85 93 90 91
 $ assists: int 12 20 23 8 14

Method 3: Using fread

If your CSV is extremely large, the fastest way to import it into R is with the fread function from the data.table package:

library(data.table)

#import data
data3 C:\Users\Bob\Desktop\data.csv")

#view structure of data
str(data3)

Classes 'data.table' and 'data.frame':  5 obs. of  3 variables:
 $ team   : chr  "'A'" "'B'" "'C'" "'D'" ...
 $ points : int  78 85 93 90 91
 $ assists: int  12 20 23 8 14

Note that in each example we used double backslashes (\) in the file path to avoid the following common error:

Error: 'U' used without hex digits in character string starting ""C:U"

Additional Resources

The following tutorials explain how to import other file types into R:

How to Import Excel Files into R
How to Import TSV Files into R
How to Import Zip Files into R
How to Import SAS Files into R
How to Import .dta Files into R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories