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 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
# load the librarylibrary(dplyr) # create dataframe with 3 columns# id,name and addressdata1=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')) # remove id and nameprint(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
# load the librarylibrary(dplyr) # create dataframe with 3 columns# id,name and addressdata1=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')) # remove id print(select(data1,-id)) # remove multiple columns- name,idprint(select(data1,-c(name,id))) |
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
# load the librarylibrary(dplyr) # create dataframe with 3 columns# id,name and addressdata1=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')) # remove column that contains naprint(select(data1,-contains('na'))) # remove column that contains reprint(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
# load the librarylibrary(dplyr) # create dataframe with 3 columns# id,name and addressdata1=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')) # remove column that starts with naprint(select(data1,-starts_with('na'))) # remove column that starts with adprint(select(data1,-starts_with('ad'))) |
Output:
Example: R program to remove a column that ends with character/substring
R
# load the librarylibrary(dplyr) # create dataframe with 3 columns # id,name and addressdata1=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')) # remove column that ends with dprint(select(data1,-ends_with('d'))) # remove column that starts with ssprint(select(data1,-ends_with('ss'))) |
Output:


