Extracting Subset of data.table Variables in R (Example Code)
In this R programming tutorial you’ll learn how to retain particular columns of a data.table.
Preparing the Example
install.packages("data.table") # Install & load data.table library("data.table") |
install.packages("data.table") # Install & load data.table library("data.table")
my_dt <- data.table(matrix(1:20, ncol = 5)) # Constructing data table in R my_dt # Showing data table in RStudio console # V1 V2 V3 V4 V5 # 1: 1 5 9 13 17 # 2: 2 6 10 14 18 # 3: 3 7 11 15 19 # 4: 4 8 12 16 20 |
my_dt <- data.table(matrix(1:20, ncol = 5)) # Constructing data table in R my_dt # Showing data table in RStudio console # V1 V2 V3 V4 V5 # 1: 1 5 9 13 17 # 2: 2 6 10 14 18 # 3: 3 7 11 15 19 # 4: 4 8 12 16 20
Example: Extract Data Table Variables
my_dt_updated <- my_dt[ , # Deleting particular columns ! c("V1", "V3", "V4"), with = FALSE] my_dt_updated # Showing new data table # V2 V5 # 1: 5 17 # 2: 6 18 # 3: 7 19 # 4: 8 20 |
my_dt_updated <- my_dt[ , # Deleting particular columns ! c("V1", "V3", "V4"), with = FALSE] my_dt_updated # Showing new data table # V2 V5 # 1: 5 17 # 2: 6 18 # 3: 7 19 # 4: 8 20
Further Resources & Related Tutorials
Below, you can find some further resources that are similar to the topic of this page.