Extract data.table Column as Vector Using Index Position in R Last Updated : 17 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The column at a specified index can be extracted using the list sub-setting, i.e. [[, operator. The double bracket operator is faster in comparison to the single bracket, and can be used to extract the element or factor level at the specified index. In case, an index more than the number of rows is specified, then an exception stating index out of bounds is returned. Single bracket [, operator cannot be used here, because it returns a subset of the data table, that is a data.table element as the output, not an atomic vector. Constant time is required to perform this operation. Example: R library("data.table") # declaring data table data_frame <- data.table(col1 = c(2,4,6), col2 = c(4,6,8), col3 = c(8,10,12), col4 = c(20,16,14)) print ("Original DataTable") print (data_frame) # extracting column 2 print ("Column 2 as a vector") vec <- data_frame[[2]] print (vec) Output [1] "Original DataTable" col1 col2 col3 col4 1: 2 4 8 20 2: 4 6 10 16 3: 6 8 12 14 [1] "Column 2 as a vector" [1] 4 6 8 Every column of the data.table can also be extracted in a separate vector, by looping over the entire data.table. ncol() method can be used to return the total number of columns in the data.table. The total time required to carry this operation is equivalent to O(n), where n are the columns. Example: R # getting required libraries library("data.table") # declaring data table data_table <- data.table(col1 = c(2,4,6), col2 = FALSE, col3 = LETTERS[1:3]) print ("Original DataTable") print (data_table) # getting number of columns cols <- ncol(data_table) # looping through columns for (i in 1:cols){ # getting ith col cat(i, "th col \n") print(data_table[[i]]) } Output [1] "Original DataTable" col1 col2 col3 1: 2 FALSE A 2: 4 FALSE B 3: 6 FALSE C 1 th col [1] 2 4 6 2 th col [1] FALSE FALSE FALSE 3 th col [1] "A" "B" "C" Comment More infoAdvertise with us Next Article Get Column Index in Data Frame by Variable Name in R M mallikagupta90 Follow Improve Article Tags : R Language R DataTable Similar Reads Split Text String in a data.table Column Using R In data manipulation tasks, especially when working with text data, you often need to split strings in a column and expand the results into multiple columns. The data.table package in R offers efficient methods to handle such operations, making it easy to perform complex data transformations. This a 3 min read Add Multiple New Columns to data.table in R In this article, we will discuss how to Add Multiple New Columns to the data.table in R Programming Language. To do this we will first install the data.table library and then load that library. Syntax: install.packages("data.table") After installing the required packages out next step is to create t 3 min read Apply Function to data.table in Each Specified Column in R In this article, we are going to see that how to apply a function to data.table in each specified column in R Programming Language. The data.table library in R is used to create datasets and represent it in an organized manner. The library can be downloaded and installed into the working space using 3 min read Unnesting a list of lists in a data frame column in R Working with data that has lists within columns is frequent when using R programming language. These lists may include various kinds of information, including other lists. But, working with these hierarchical lists can be difficult, especially if we wish to analyze or visualize the data. A list of l 6 min read Get Column Index in Data Frame by Variable Name in R R is an open-source programming language that is used as a statistical software and data analysis tool. In R Programming Language we can work on specific columns based on their names. In this article, we will learn different methods to extract the Get Column Index in Data Frame by Variable Name in R 5 min read Hide Certain Columns in a Responsive Data Table Using DT Package in R The DT package in R Shiny provides a powerful and interactive way to display data tables in web applications. One useful feature is the ability to hide certain columns based on user interactions or display requirements. In this guide, weâll explore how to hide columns in a responsive data table usin 3 min read Like