How to Remove a Column using Dplyr package in R
Last Updated :
07 Aug, 2023
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 name
Here we will use select() method to select and remove column by its name.
Syntax:
select(dataframe,-column_name)
Here, dataframe is the input dataframe and column_name is the column in the dataframe to be removed
To remove multiple columns:
Syntax:
select(dataframe,-c(column1,column2,.,column n))
Here, dataframe is the input dataframe and columns are the columns in the dataframe to be removed.
Example: R program to remove column by its name
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,- c (name,id)))
|
Output:

Remove columns by position
Here we will remove column(s) using column index or position. We will use the select() method to select a column by removing its position
Note: Index starts with 1
Syntax:
select(dataframe,-index)
Here, dataframe is the input dataframe and index is the column position in the dataframe to be removed.
To remove multiple columns:
Syntax:
select(dataframe,-c(index1,index2,.,index n))
Here, dataframe is the input dataframe and indexes are the column positions in the dataframe to be removed.
Example: R program to remove column by index
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,- c (1,2)))
|
Output:

Remove column which contains a value or matches a pattern
Let’s discuss how to remove the column that contains the character or string.
Method 1: Using contains()
contains() removes the column that contains the given substring.
Syntax:
select(dataframe,-contains(‘sub_string’))
Here, dataframe is the input dataframe and sub_string is the string present in the column name will be removed.
Method 2: Using matches()
matches() removes the column that contains the given substring.
Syntax:
select(dataframe,-matches(‘sub_string’))
Here, dataframe is the input dataframe and sub_string is the string present in the column name will be removed.
Example: R program that removes column using contains() method
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 ( 'na' )))
print ( select (data1,- contains ( 're' )))
|
Output:

Remove 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: R program to remove a column 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 ( 'na' )))
print ( select (data1,- starts_with ( 'ad' )))
|
Output:

Example: R program to remove a column that ends with 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,- ends_with ( 'd' )))
print ( select (data1,- ends_with ( 'ss' )))
|
Output:

Similar Reads
How to Remove a Column by name and index using Dplyr Package in R
In this article, we are going to remove columns by name and index in the R programming language using dplyr package. Dataset in use: Remove a column by using column name We can remove a column with select() method by its column name. Syntax: select(dataframe,-column_name) Where, dataframe is the inp
2 min read
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
How To Remove A Column In R
R is a versatile language that is widely used in data analysis and statistical computing. A common task when working with data is removing one or more columns from a data frame. This guide will show you various methods to remove columns in R Programming Language using different approaches and provid
4 min read
Remove Rows with NA Using dplyr Package in R
NA means Not Available is often used for missing values in a dataset. In Machine Learning NA values are a common problem and if not treated properly can create severe issues during data analysis. NA is also referred as NaN which means Not a number. To understand NA values we can think of an admissio
5 min read
Drop multiple columns using Dplyr package in R
In this article, we will discuss how to drop multiple columns using dplyr package in R programming language. Dataset in use: Drop multiple columns by using the column name We can remove a column with select() method by its column name Syntax: select(dataframe,-c(column_name1,column_name2,.,column_na
4 min read
Sum Across Multiple Rows and Columns Using dplyr Package in R
In this article, we are going to see how to sum multiple Rows and columns using Dplyr Package in R Programming language. The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following comman
3 min read
Remove duplicate rows based on multiple columns using Dplyr in R
In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. Dataframe in use: lang value usage 1 Java 21 21 2 C 21 21 3 Python 3 0 4 GO 5 99 5 RUST 180 44 6 Javascript 9 48 7 Cpp 12 53 8 Java 21 21 9 Julia 6 6 10 Typescript 0 8 11 Pyth
4 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
Reorder the column of dataframe in R using Dplyr
In this article, we will discuss how to rearrange or reorder the column of the dataframe using dplyr package in R Programming Language. Creating Dataframe for demonstration: C/C++ Code # load the package library(dplyr) # create the dataframe with three columns # id , department and salary with 8 row
4 min read
Remove Duplicate rows in R using Dplyr
In this article, we are going to remove duplicate rows in R programming language using Dplyr package. Method 1: distinct() This function is used to remove the duplicate rows in the dataframe and get the unique data Syntax: distinct(dataframe) We can also remove duplicate rows based on the multiple c
3 min read