How to Import CSV Files into R [3 Ways]

Here are three ways to import CSV files into R:

  1. Using the read.csv() function from base R
  2. Using the read_csv() function from the readr package
  3. Using the fread() function from data.table package

Method 1: Using the read.csv() function from base R

The read_csv() function comes with base R, so no additional libraries are needed, and it is easy to use.

Syntax

data <- read.csv("file_path.csv")

Example

Let’s say we are working with mlb_players.csv data. 

# Load the readxl package
library(readxl)

# Read an Excel file into a data frame
my_data <- read.csv("mlb_players.csv")

# Display the first few rows of the data
head(my_data)

Output

Using the read.csv() function from base R

Method 2: Using read_csv() from the readr package

The read_csv() function from the readr package is Faster than the read.csv() function. It provides more options for specifying data types, date formats, etc. As a part of the tidyverse, it integrates well with other tidyverse packages like dplyr, ggplot2, etc.

Syntax

library(readr)
data <- read_csv("file_path.csv")

Example

# Load the readxl package
library(readr)

# Read an Excel file into a data frame
my_data <- read_csv("mlb_players.csv")

# Display the first few rows of the data
head(my_data)

Output

Using read_csv() from the readr package

Method 3: Using fread() from the data.table package

The fread() function from the data.table package is used in extremely large datasets. It can read data in chunks. Automatically detects separators, headers, data types, etc.

Syntax

library(data.table) 

data <- fread("file_path.csv")

Example

# Load the readxl package
library(data.table)

# Read an Excel file into a data frame
my_data <- fread("mlb_players.csv")

# Display the first few rows of the data
head(my_data)

Output

Using fread() from the data.table package

When to use which method and why

  1. Base R’s read.csv():

    • When to Use: Ideal for beginners or quick, ad-hoc tasks where you don’t need to load additional libraries.
    • Why: Easy to use, no need for additional libraries.
  2. readr’s read_csv():
    • When to Use: When working within the tidyverse ecosystem and needing more speed and flexibility than base R.
    • Why: Faster, more flexible, and integrates well with other tidyverse packages.
  3. data.table’s fread():

    • When to Use: When you have huge datasets and need the fastest reading times, or you already use data.table for its other features.
    • Why: Extremely fast, especially for large datasets, and provides advanced capabilities like chunk reading.

That’s it!

    Related posts

    Import Excel Files into R

    Export DataFrame to CSV in R

    Read CSV File into DataFrame in R

    Leave a Comment