Generating an Empty data.table with Column Names in R (2 Examples)

In this article, I’ll illustrate how to generate a blank data.table in the R programming language.

Setting up the Examples

We install and load the data.table package. For general information about the package data.table, we recommend you to take a look at this page.

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

For Example 2, we use the iris dataset. We therefore load the data and convert it to a data.table.

data(iris)                                   # Load iris data set
iris_DT_1 <- data.table::copy(iris)          # Replicate the iris data set
iris_DT_1 <- setDT(iris_DT_1)                # Convert to data.table
head(iris_DT_1)                              # Printing the data head
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1:          5.1         3.5          1.4         0.2  setosa
# 2:          4.9         3.0          1.4         0.2  setosa
# 3:          4.7         3.2          1.3         0.2  setosa
# 4:          4.6         3.1          1.5         0.2  setosa
# 5:          5.0         3.6          1.4         0.2  setosa
# 6:          5.4         3.9          1.7         0.4  setosa

Example 1: Generate empty data.table

Let's create a data.table from scratch. We only select the names and types of the columns and create it with data.table() as shown below.

empty_DT <- data.table("A" = character(),    # Generating an empty data.table
                       "B" = logical(),
                       "C" = double(),
                       "D" = factor())

str(empty_DT)                                # See the structure of the data
# Classes ‘data.table’ and 'data.frame':	0 obs. of  4 variables:
#  $ A: chr 
#  $ B: logi 
#  $ C: num 
#  $ D: Factor w/ 0 levels: 
#  - attr(*, ".internal.selfref")= 

empty_DT                                     # Printing the data
# Empty data.table (0 rows and 4 cols): A,B,C,D

You see that there are zero rows in the data. For information about the different column types available in R, take a look at the description here.

Example 2: Set all values in a data.table to NA

Alternatively to generating a data.table from scratch, we can also take a data.table and set all its input values equal to NA. Below, we set all values in the iris data to NA.

iris_DT_2 <- data.table::copy(iris_DF_1)     # Replicate the iris data set
iris_DT_2[ !is.na(iris_DT_2) ] <- NA         # Set all values to NA
str(iris_DT_2)                               # See the structure of the data
# 'data.frame':	150 obs. of  5 variables:
# $ Sepal.Length: num  NA NA NA NA NA NA NA NA NA NA ...
# $ Sepal.Width : num  NA NA NA NA NA NA NA NA NA NA ...
# $ Petal.Length: num  NA NA NA NA NA NA NA NA NA NA ...
# $ Petal.Width : num  NA NA NA NA NA NA NA NA NA NA ...
# $ Species     : Factor w/ 3 levels "setosa","versicolor",..: NA NA NA NA NA NA NA NA NA NA ...
head(iris_DT_2)                              # Printing the data head
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#   1           NA          NA           NA          NA    
#   2           NA          NA           NA          NA    
#   3           NA          NA           NA          NA    
#   4           NA          NA           NA          NA    
#   5           NA          NA           NA          NA    
#   6           NA          NA           NA          NA    

Further Resources

Please find some related R programming language tutorials on topics such as merging, matrices, and variables in the following list:

 

Anna-Lena Wölwer R Programming & Survey Statistics

Note: This article was created in collaboration with Anna-Lena Wölwer. Anna-Lena is a researcher and programmer who creates tutorials on statistical methodology as well as on the R programming language. You may find more info about Anna-Lena and her other articles on her profile page.

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.
You need to agree with the terms to proceed

Menu
Top