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 readr library, and data.table library to create a data frame by reading CSV content from a URL. The CSV format is the simplest way to store scientific, analytical, or any structured data (two-dimensional with rows and columns). Data in a CSV file is typically separated by a delimiter, most commonly a comma (,), but you can also use other characters like the pipe (|), tab (\t), etc.

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 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 with a comma delimiter. However, you can specify any custom delimiter by using the sep argument.


# 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

Finally, let’s use the data.table package to read CSV content from a URL in R. Using this is the best approach when you are reading very large data sets.


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

Conclusion

In this article, you have learned how to read or load a CSV from a web URL using read.csv(), read.table() and finally read_csv() from the readr package.

References