Write & Read CSV File as data.table in R (2 Examples)

 

In this article, you’ll learn how to export a data.table as a CSV file and how to import a CSV file as a data.table in the R programming language. You are not familiar with data.table in R? Have a look at our blog post about data.table here. There are also numerous posts about data.table on Stack Overflow.

The article will consist of the following contents:

Let’s dive into it:

 

Example Data & Add-On Packages

We first need to install and load the data.table package to RStudio:

install.packages("data.table")       # Install & load data.table
library("data.table")

We’ll also have to create some example data:

dt_1 <- data.table( Var1 = 1:200,    # Generate example data
                    Var2 = rep(month.abb[1:10], 20),
                    Var3 = seq(0, 1, length.out = 200) )
 
head(dt_1)                           # Print head of data

 

table 1 data frame read csv as data table

 

Have a look at the previous table. It visualizes the top six lines of our example data, and that our data contains three columns. For the data creation we used some functions. Find out more about the sequence function seq() here and the month names month.abb[] here.

To save and load the data, we define a path in the following. You can change it to a path of your choice, e.g. the download folder.

path <- "C:/Users/..." # Set the path where you want to store the example data

 

Example 1: Save data.table as CSV

Example 1 demonstrates how to export the previously defined data.table dt_1 as a CSV file using write.csv(). With argument file = “…” you determine where the data is stored.

?write.csv                           # Show help file
 
write.csv(dt_1,                      # Save data as CSV file
         file = paste0(path, "test_csv_file.csv"))

Look at the path folder which you defined. There you can now see the CSV file. Take a look at the documentation of write.csv() if you want to e.g. change the decimal point from “.” to “,” and for further options.

 

Example 2: Load CSV as data.table

This example shows how to import the saved CSV file as a data.table in R with function read.csv(). Again, make sure you set path to the folder where your CSV file is stored.

loaded_data <- read.csv(file = paste0(path, "test_csv_file.csv"))

The CSV file is imported as loaded_data. Take a look at the class of the file.

class(loaded_data)                   # Get object class
# [1] "data.frame"

It is a data.frame. To convert it to a data.table, use the following command setDT().

setDT(loaded_data)                   # Transform to data.table
class(loaded_data)                   # Get object class
# [1] "data.table" "data.frame"

As a result: The CSV file was loaded in R and is now a data.table object.

 

Video & Further Resources

I have recently published a video on my YouTube channel, which illustrates the R syntax of this tutorial. You can find the video below:

 

The YouTube video will be added soon.

 

In addition, you might read some related posts on this website.

 

In this article, you have learned how to save a data.table as a CSV file and how to load a CSV file as a data.table in R. Please let me know in the comments section, in case you have additional questions. Furthermore, please subscribe to my email newsletter to get updates on the newest tutorials.

 

Anna-Lena Wölwer Survey Statistician & R Programmer

This page was created in collaboration with Anna-Lena Wölwer. Have a look at Anna-Lena’s author page to get further details about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top