You are currently viewing How to Read CSV From URL in R?

How can I read a CSV from a Web URL to an R data frame? R provides a method from the R base library, from the readr library, and data.table library to create a data frame by reading CSV content from a URL. The CSV format is the easiest method to store scientific, analytical, or any structured data in a two-dimensional format with rows and columns. In a CSV file, data is typically separated by a delimiter, most commonly a comma (,), although other characters like the pipe (|), tab (\t), etc., can also be used.

In this article, I will explore how to create an R data frame by reading CSV content from a URL.

Quick Examples of Read CSV From URL

Below are quick examples of reading a CSV from a URL.


# Quick examples

# Read CSV into DataFrame
df <- read.csv('https://sparkbyexamples.com.com/csv_data.csv')

# Read with custom delimiter
df = read.csv('https://sparkbyexamples.com.com/csv_data.csv',sep=',')

# Read without header
df = read.csv('https://sparkbyexamples.com.com/csv_data.csv',header=FALSE)

# Using encoding
read_csv = read.csv('https://sparkbyexamples.com.com/csv_data.csv', encoding='utf-8')

# Using data.table
library(data.table)
df <- fread('https://sparkbyexamples.com.com/csv_data.csv')

# Using readr
library(readr)
data <- read_csv('https://sparkbyexamples.com.com/csv_data.csv')

2. Read CSV from a URL in R

To read CSV content from a URL into a data frame use the R base function read.csv(). Following is the syntax of the read.csv() function in R. This method is also used to read a CSV file from disk into data frame.


# Syntax of read.csv()
read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", …)

If you have the URL with CSV content comma-separated use the read.csv(), by default this method considers the content in comma-separated format.


# Read CSV into DataFrame
df <- read.csv('https://sparkbyexamples.com.com/csv_data.csv')
print(df)

Yields below output.


# Output
  id name        dob gender
1 10  sai 1990-10-02      M
2 NA  ram 1981-03-24       
3 -1 <NA> 1987-06-14      F
4 13      1985-08-16   <NA>

3. Read with Custom Delimiter from the URL

By default, this reads the content using a comma as the delimiter. However, you can use the sep argument to specify any custom delimiter.


# Usage of sep param
read_csv = read.csv('https://sparkbyexamples.com.com/csv_data.csv',sep=',')
print(read_csv)

4. Read without Header from URL

Sometimes you may have a URL where the data doesn’t contain the header, if so use header=FALSE. Let’s take another URL where content doesn’t contain a header row (column names) and load it into the data frame.


# Use header=False
read_csv = read.csv('https://sparkbyexamples.com.com/file_noheader.csv', header=FALSE)
print(read_csv)

Yields below output.


# Output
  V1   V2         V3     V4
1 10  sai 1990-10-02      M
2 NA  ram 1981-03-24       
3 -1 <NA> 1987-06-14      F
4 13      1985-08-16   <NA>

Note that the default column names it assigns as V1, V2, V3, and V4. To rename column names on data frame to your own use colnames().


# Set column names
colnames(read_csv) = c('id','name','dob','gender')
print(read_csv)

5. CSV encoding

If a CSV you are reading from URL is in another encoding then use encoding=UTF-8 argument. This reads data as UTF-8 into DataFrame.


# Use UTF-8 encoding
read_csv = read.csv('https://sparkbyexamples.com.com/file_noheader.csv', encoding='utf-8')
print(read_csv)

6. Use read_csv() from readr Package

If you’re dealing with sizable files, utilizing the read_csv() function from the readr package is advisable. Since readr is an external library, you’ll need to install it initially using install.packages('readr'). Once the installation process is finished, you can load the readr library to access the read_csv() function. To load a library in R, use the library("readr").


# Load readr
library("readr")

# Read CSV into DataFrame
read_csv = read_csv('https://sparkbyexamples.com.com/csv_data.csv')
print(read_csv)

7. Using data.table

Lastly, let’s utilize the data.table package to read CSV content from a URL in R. This approach is optimal for handling very large datasets.


# Using data.table
library(data.table)
df <- fread('https://sparkbyexamples.com.com/csv_data.csv')

Conclusion

This article has shown you how to read or load a CSV file from a web URL using the read.csv() and read.table() functions, as well as the read_csv() function from the readr package.

References