How to Select Specific Columns in R dataframe? Last Updated : 21 Dec, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to select specific columns from a data frame in the R Programming Language. Selecting specific Columns Using Base R by column nameIn this approach to select a specific column, the user needs to write the name of the column name in the square bracket with the name of the given data frame as per the requirement to get those specific columns needed by the user. Syntax: data_frame R # Creating DataFramegfg <- data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific Columns Using Base# R by column namegfg[c('b', 'd', 'e')] Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific Columns Using Base R by column indexIn this approach to select the specific columns, the user needs to use the square brackets with the data frame given, and. With it, the user also needs to use the index of columns inside of the square bracket where the indexing starts with 1, and as per the requirements of the user has to give the required column index to inside the brackets Syntax: data_frame R # Creating DataFramegfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific Columns Using Base R # by column indexgfg[c(2, 4, 5)] Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific columns by subsetting data by column nameIn this method of selecting specific columns by subsetting data, the user needs to do the specification of a character vector containing the names of the columns to extract, the user has to enter the vector of the characters which corresponds to the column name in the square bracket with the data frame Syntax: data_frame[,c(column_name_1,column_name_2,...)] R # Creating DataFramegfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific columns by subsetting # data by column namegfg[, c('b', 'd', 'e')] Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific columns by subsetting data by column indexIn this method of selecting specific columns by subsetting data, the user needs to do the specification of an integer vector containing the index of the columns to extract, the user has to enter the vector of the indexes which corresponds to the column index in the square bracket with the data frame Syntax: data_frame[,c(column_index_1,column_index_2,...)] R # Creating DataFramegfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific columns by subsetting data# by column index:gfg[, c(2, 4, 5)] Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific columns by Subsetting Data with select Argument of subset FunctionSubset function: This function will be returning the subsets of data frames that meet conditions. Syntax: subset(x, subset, select, drop = FALSE, …) Parameters: x: object to be subsetted.subset: logical expression indicating elements or rows to keep: missing values are taken as false.select: expression, indicating columns to select from a data frame.drop: passed on to [ indexing operator.…: further arguments to be passed to or from other methods. R # Creating DataFramegfg <- data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific columns by Subsetting # Data with select Argument of subset Functionsubset(gfg, select=c('b', 'd', 'e')) Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific columns using dplyr package by column nameIn this approach to select the specific columns of the given data frame, the user needs first install and import the dplyr package in the working R console of the user and then call the select function and pass the name of the required columns as the argument of this function Syntax: data_frame %>% select(column_name_1,column_name_2,...) R # Importing dplyr librarylibrary("dplyr") # Creating DataFramegfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific columns using dplyr # package by column namegfg %>% select(b, d, e) Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4Selecting specific columns using dplyr package by column indexIn this approach to select the specific columns of the given data frame, the user needs first install and import the dplyr package in the working R console of the user and then call the select function and pass the index of the required columns as the argument of this function Syntax: data_frame %>% select(column_index_1,column_index_2,...) R # Importing dplyr librarylibrary("dplyr") # Creating DataFramegfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9), b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3), c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7), d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7), e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4)) # Selecting specific columns using dplyr # package by column indexgfg %>% select(2, 4, 5) Output: b d e1 1 4 32 8 6 13 6 8 64 8 4 45 6 6 86 7 4 97 4 8 78 1 9 89 7 8 910 3 7 4 Comment More infoAdvertise with us Next Article How to Switch Two Columns in R DataFrame? geetansh044 Follow Improve Article Tags : R Language R Programs R DataFrame-Programs R-DataFrame Similar Reads How to Select DataFrame Columns by Index in R? In this article, we will discuss how to select columns by index from a dataframe in R programming language. Note: The indexing of the columns in the R programming language always starts from 1. Method 1: Select Specific Columns By Index with Base R Here, we are going to select columns by using index 2 min read How to Switch Two Columns in R DataFrame? In this article, we will discuss how to switch two columns in dataframe in R Programming Language. Let's create the dataframe with 6 columns [GFGTABS] R # create a dataframe data = data.frame(column1=c(1, 2, 3), column2=c(4, 5, 6), column3=c(2, 3, 4), column4=c(4, 5, 6), column5=c(5, 3, 2), column6= 1 min read How to add column to dataframe in R ? In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n) Here c() function is a vec 2 min read How to select multiple DataFrame columns by name in R ? In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Sy 1 min read Select DataFrame Column Using Character Vector in R In this article, we will discuss how to select dataframe columns using character vectors in R programming language. Data frame in use: To extract columns using character we have to use colnames() function and the index of the column to select is given with it using []. The approach is sort of the sa 2 min read How to Delete Multiple Columns in R DataFrame? In this article, we will discuss how to delete multiple columns in R Programming Language. We can delete multiple columns in the R dataframe by assigning null values through the list() function. Syntax: data[ , c('column_name1', 'column_name2',...........,'column_nam en)] <- list(NULL) where, dat 1 min read How to convert table to dataframe in R? In this article, we will discuss how to convert a given table to a dataframe in the R programming language. Functions Usedas.data.frame.matrix() will be taking the table as its parameter and will return the dataframe back to the user. Syntax: as.data.frame.matrix(x) Parameter: x: name of the table w 1 min read How to split DataFrame in R In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r 4 min read How to add a prefix to column names in R DataFrame ? In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for 4 min read How to add suffix to column names in R DataFrame ? Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the 4 min read Like