Changing data.table Printing Options in R (3 Examples)

In this article, I’ll explain how to change the printing options of data.table in the R programming language.

Preparing the Examples

We first install and load the data.table package. If you do not know data.table in R, check its Github page here for more information, especially the getting started page. You can also find information on Gitlab.

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

To illustrate the possibilities of printing data.tables, we use the built-in iris dataset.

data(iris)                                       # Load iris data set
head(iris)
#   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

We copy the data as a data.table, called iris_dt.

iris_dt <- data.table(data.table::copy(iris))    # Copy data as data.table

Example 1: Displaying the Printing Defaults of data.table

With ?print.data.table, you can see the following output in the help panel in R Studio.

?print.data.table
# ## S3 method for class 'data.table'
# print(x,
#       topn = getOption("datatable.print.topn"), # default: 5
#       nrows = getOption("datatable.print.nrows"), # default: 100
#       class = getOption("datatable.print.class"), # default: FALSE
#       row.names = getOption("datatable.print.rownames"), # default: TRUE
#       col.names = getOption("datatable.print.colnames"), # default: "auto"
#       print.keys = getOption("datatable.print.keys"), # default: FALSE
#       trunc.cols = getOption("datatable.print.trunc.cols"), # default: FALSE
#       quote = FALSE,
#       timezone = FALSE, ...)

It shows the default options used for printing a data.table object. For example, class=FALSE determines that the classes of the columns are not printed.

Example 2: Printing a data.table in Different Ways

We first show the default way of printing a data.table.

iris_dt                                          # Print data
     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
 ---                                                            
146:          6.7         3.0          5.2         2.3 virginica
147:          6.3         2.5          5.0         1.9 virginica
148:          6.5         3.0          5.2         2.0 virginica
149:          6.2         3.4          5.4         2.3 virginica
150:          5.9         3.0          5.1         1.8 virginica

As the default of topn is 5, see the output of ?print.data.table above, only the first and last five data rows are printed. Now, we use function print(), but change the printing argument topn=3 to show only the first and last three data rows.

print(iris_dt, topn = 3)
     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
 ---                                                            
148:          6.5         3.0          5.2         2.0 virginica
149:          6.2         3.4          5.4         2.3 virginica
150:          5.9         3.0          5.1         1.8 virginica

To make another example, with quote=TRUE, the output appears in quotes and with row.names=FALSE, the row names are not printed.

print(iris_dt, quote = TRUE, row.names = FALSE)
    "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"   "Species"
             "5.1"         "3.5"          "1.4"         "0.2"    "setosa"
             "4.9"         "3.0"          "1.4"         "0.2"    "setosa"
             "4.7"         "3.2"          "1.3"         "0.2"    "setosa"
             "4.6"         "3.1"          "1.5"         "0.2"    "setosa"
             "5.0"         "3.6"          "1.4"         "0.2"    "setosa"
---             ""            ""             ""            ""          ""
             "6.7"         "3.0"          "5.2"         "2.3" "virginica"
             "6.3"         "2.5"          "5.0"         "1.9" "virginica"
             "6.5"         "3.0"          "5.2"         "2.0" "virginica"
             "6.2"         "3.4"          "5.4"         "2.3" "virginica"
             "5.9"         "3.0"          "5.1"         "1.8" "virginica"

Take a look at the documentation found via ?print.data.table to see the meaning of the other options.

Example 3: Changing the Global Printing Options of data.table

Instead of setting specific printing options as arguments within print(), we can change the global printing options. As an example, in the following we set datatable.print.class = TRUE and datatable.print.topn = 7.

options(datatable.print.class = TRUE,            # Changing global options for data.table
        datatable.print.topn = 2)

Now, function print() shows the data.table information with the updated global options.

print(iris_dt)
     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
            <num>       <num>        <num>       <num>    <fctr>
  1:          5.1         3.5          1.4         0.2    setosa
  2:          4.9         3.0          1.4         0.2    setosa
 ---                                                            
149:          6.2         3.4          5.4         2.3 virginica
150:          5.9         3.0          5.1         1.8 virginica
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.

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