Removing Columns of data.table by Index in R (2 Examples)

In this tutorial, I’ll illustrate how to exclude certain columns from a data.table by indexing them in the R programming language.

Setting up the Examples

First, we need to install and load the data.table package.

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

We use the iris dataset to illustrate how to delete certain data.table columns by their indices.

data(iris)                                # Load iris data set
iris_DT <- data.table::copy(iris)         # Replicate iris data set
setDT(iris_DT)                            # Convert iris to a data.table

head(iris_DT)                             # Print the head of the 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
# 6:          5.4         3.9          1.7         0.4  setosa

Example 1: Removing Column By Indexing

We exclude column number three (Petal.Length) from the data using the following code.

iris_DT_2 <- data.table::copy(iris_DT)    # Replicate data set iris_DT
iris_DT_3 <- iris_DT_2[ , -c(3) ]         # Remove column 3

head(iris_DT_3)                           # Print the head of the new data
#    Sepal.Length Sepal.Width Petal.Width Species
# 1:          5.1         3.5         0.2  setosa
# 2:          4.9         3.0         0.2  setosa
# 3:          4.7         3.2         0.2  setosa
# 4:          4.6         3.1         0.2  setosa
# 5:          5.0         3.6         0.2  setosa
# 6:          5.4         3.9         0.4  setosa

Example 2: Removing Several Columns By Indexing

We delete columns number three (Petal.Length) and five (Species) from the data with the following lines of R code.

iris_DT_4 <- data.table::copy(iris_DT)    # Replicate data set iris_DT
iris_DT_5 <- iris_DT_4[ , -c(3, 5) ]      # Remove columns 3 and 5
head(iris_DT_5)                           # Print the head of the new data
#    Sepal.Length Sepal.Width Petal.Width
# 1:          5.1         3.5         0.2
# 2:          4.9         3.0         0.2
# 3:          4.7         3.2         0.2
# 4:          4.6         3.1         0.2
# 5:          5.0         3.6         0.2
# 6:          5.4         3.9         0.4

Further Resources

In the following, you may find some further resources that are similar to the topic of this post:

 

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