Select Subset of DataTable Columns in R Last Updated : 23 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to select a subset of data table columns in R programming language. Let's create a data table using a matrix. First, we need to load data.table package in the working space. Installation install.packages("data.table") Loading library("data.table") Dataset in use: Method 1: Using [] We can select a subset of datatable columns by index operator - [] Syntax: datatable[ , c(columns), with = FALSE] Where, datatable is the input data tablecolumns are the columns in the datatable to be selectedwith =FALSE is an optional parameter Example: R program to select subset of columns from the data table R # load data.table package library("data.table") # create data table with matrix with 20 elements # 4 rows and 5 columns data= data.table(matrix(1:20, nrow=4,ncol = 5)) # display the subset that include v1 and v3 columns print(data[ , c("V1", "V3"), with = FALSE]) # display the subset that include v1 , v2 and v3 columns print(data[ , c("V1","V2", "V3"), with = FALSE]) # display the subset that include v2,v3,v4 and v5 columns print(data[ , c("V2", "V3","V4","V5"), with = FALSE]) Output: Method 2: Using ! Using ! operator before columns can be enough to get the job done by this approach. Here we are not including the subset that is selected from the data table Syntax: datatable[ , !c(columns), with = FALSE] where, datatable is the input data tablecolumns are the columns in the datatable to be selected Example: R program to select columns from datatable R # load data.table package library("data.table") # create data table with matrix with 20 elements # 4 rows and 5 columns data= data.table(matrix(1:20, nrow=4,ncol = 5)) # display the subset that exclude v1 and v3 columns print(data[ , !c("V1", "V3"), with = FALSE]) # display the subset that exclude v1 , v2 and v3 columns print(data[ , !c("V1","V2", "V3"), with = FALSE]) # display the subset that exclude v2,v3,v4 and v5 columns print(data[ , !c("V2", "V3","V4","V5"), with = FALSE]) Output: Comment More infoAdvertise with us Next Article R Shiny: Set DataTable Column Width G gottumukkalabobby Follow Improve Article Tags : R Language R DataTable Similar Reads How to select a subset of DataFrame in R In general, when we were working on larger dataframes, we will be only interested in a small portion of it for analyzing it instead of considering all the rows and columns present in the dataframe. Creation of Sample Dataset Let's create a sample dataframe of Students as follows R student_details 2 min read R Shiny: Set DataTable Column Width When building interactive web applications with R Shiny, presenting data in a well-organized and visually appealing manner is crucial. One popular way to display data is using the DataTable widget from the DT package. This article helps to understand the setting of column widths in a DataTable to en 5 min read DataFrame Rows & Column Segment in R The process of extracting the row and column information in a dataset by simply using the index or slice operator is known as Slicing. In R Programming, the rows and columns can be sliced in two ways either by using an index or using the name of the row and column. The slice operator is much more us 2 min read Select variables (columns) in R using Dplyr In this article, we are going to select variables or columns in R programming language using dplyr library. Dataset in use: Select column with column name Here we will use select() method to select column by its name Syntax: select(dataframe,column1,column2,.,column n) Here, data frame is the input 5 min read Import Only Selected Columns of Data from CSV in R In this article, we will be looking at two different approaches to import selected columns of the Data from a CSV file in the R programming language. Method 1: Using read.table() function In this method of only importing the selected columns of the CSV file data, the user needs to call the read.tabl 2 min read How to Handle "undefined columns selected" in R? In this article, we will discuss how to handle "undefined columns selected" error in R Programming Language. This error is specific to dataframe in R. This type of error will occur when we select a subset of a data frame and forget to add a comma. Example: Check the error in the dataframe Here we cr 1 min read Like