Select variables (columns) in R using Dplyr
Last Updated :
21 Jul, 2021
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 dataframe and columns are the columns in the dataframe to be displayed
Example 1: R program to select columns
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' ,
'sravan' , 'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' ,
'guntur' , 'hyd' , 'tenali' , 'hyd' ))
print ( select (data1,id))
print ( select (data1,name))
|
Output:

Example 2: R program to select multiple columns
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' ,
'sravan' , 'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' ,
'guntur' , 'hyd' , 'tenali' , 'hyd' ))
print ( select (data1,id,name,address))
|
Output:

Select column(s) by position
We can also use the column position and get the column using select() method. Position starts with 1.
Syntax:
select(dataframe,column1_position,column2_position,.,column n_position)
where, dataframe is the input dataframe and column position is an column number
For selecting multiple columns we can use range operator “;” to select columns by their position
Syntax:
select(dataframe,start_position:end_position)
where, dataframe is the input dataframe, start_position is a column number starting position and end_position is a column number ending position
Example 1: R program to select particular column by column position
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' ,
'sravan' , 'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' ,
'guntur' , 'hyd' , 'tenali' , 'hyd' ))
print ( select (data1,1))
print ( select (data1,3))
|
Output:

Example 2: R program to select multiple columns by positions
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1,1,2))
|
Output:

Example 3: R program to select multiple columns by position with range operator
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1,1:3))
|
Output:

Select column which contains a value or matches a pattern
Here, we will display the column values based on values or pattern present in the column
Method 1: Using contains()
Display the column that contains the given sub string
Syntax:
select(dataframe,contains(‘sub_string’))
Here, dataframe is the input dataframe and sub_string is the string present in the column name
Example: R program to select column based on substring
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1, contains ( 'am' )))
print ( select (data1, contains ( 'd' )))
print ( select (data1, contains ( 'dd' )))
|
Output:

Method 2: Using matches()
It will check and display the column that contains the given sub string
select(dataframe,matches(‘sub_string’))
Here, dataframe is the input dataframe and sub_string is the string present in the column name
Example: R program to select column based on substring
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1, matches ( 'am' )))
print ( select (data1, matches ( 'd' )))
print ( select (data1, matches ( 'dd' )))
|
Output:

Select column which starts with or ends with certain character
Here we can also select columns based on starting and ending characters.
- starts_with() is used to return the column that starts with the given character.
Syntax:
select(dataframe,starts_with(‘substring’))
Where, dataframe is the input dataframe and substring is the character/string that starts with it
- ends_with() is used to return the column that ends with the given character.
Syntax:
select(dataframe,ends_with(‘substring’))
where, dataframe is the input dataframe and substring is the character/string that ends with it
Example 1: R program to display columns that starts with a character/substring
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1, starts_with ( 'n' )))
print ( select (data1, starts_with ( 'add' )))
|
Output:

Example 2: R program to select column that ends with a given string or character
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' , 'vijayawada' ,
'vijayawada' , 'guntur' , 'hyd' , 'tenali' , 'hyd' ))
print ( select (data1, ends_with ( 'ss' )))
print ( select (data1, ends_with ( 'd' )))
|
Output:

Select all columns
We can select all the columns in the data frame by using everything() method.
Syntax:
select(dataframe,everything())
Example: R program to select all columns
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5,6,7,1,4,2),
name= c ( 'sravan' , 'ojaswi' , 'bobby' , 'gnanesh' ,
'rohith' , 'pinkey' , 'dhanush' , 'sravan' ,
'gnanesh' , 'ojaswi' ),
address= c ( 'hyd' , 'hyd' , 'ponnur' , 'tenali' ,
'vijayawada' , 'vijayawada' , 'guntur' ,
'hyd' , 'tenali' , 'hyd' ))
print ( select (data1, everything ()))
|
Output:

Similar Reads
Rename the column name in R using Dplyr
In this article, we are going to rename the column name using dplyr package in the R programming language. Dataset in use: Method 1: Using rename() This method is used to rename the columns in the dataframe Syntax: rename(dataframe,new_columnname=old_column,.............,name,new_columnname=old_colu
3 min read
Dplyr - Groupby on multiple columns using variable names in R
The group_by() method is used to group the data contained in the data frame based on the columns specified as arguments to the function call. The group_by() function takes as an argument, the across and all of the methods which has to be applied on the specified grouping over all the columns of the
2 min read
Group by one or more variables using Dplyr in R
The group_by() method is used to divide and segregate date based on groups contained within the specific columns. The required column to group by is specified as an argument of this function. It may contain multiple column names. Syntax: group_by(col1, col2, ...) Example 1: Group by one variable C/C
2 min read
Select Subset of DataTable Columns in R
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:
2 min read
Filter multiple values on a string column in R using Dplyr
In this article we will learn how to filter multiple values on a string column in R programming language using dplyr package. Method 1: Using filter() method filter() function is used to choose cases and filtering out the values based on the filtering conditions. Syntax: filter(df, condition) Parame
3 min read
Summarise multiple columns using dplyr in R
In this article, we will discuss how to summarise multiple columns using dplyr package in R Programming Language, Method 1: Using summarise_all() method The summarise_all method in R is used to affect every column of the data frame. The output data frame returns all the columns of the data frame whe
3 min read
Dplyr - Find Mean for multiple columns in R
In this article, we will discuss how to calculate the mean for multiple columns using dplyr package of R programming language. Functions in useThe mutate() method adds new variables and preserves existing ones. It is used to carry out addition of more variables. The original sequence of rows and col
3 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
How to Remove a Column using Dplyr package in R
In this article, we are going to remove a column(s) in the R programming language using dplyr library. Dataset in use: Remove column using column nameHere we will use select() method to select and remove column by its name. Syntax: select(dataframe,-column_name) Here, dataframe is the input datafram
3 min read
Rank variable by group using Dplyr package in R
In this article, we are going to see how to rank the variable by group using dplyr in R Programming Language. The dplyr package in R is used to perform mutations and data manipulations in R. It is particularly useful for working with data frames and data tables. The package can be downloaded and ins
2 min read