How to Remove a Column by name and index using Dplyr Package in R
Last Updated :
21 Jul, 2021
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 input dataframe and column_name is the name of the column to be removed.
Example: R program to remove a column
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
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'))
# remove name column
print(select(data1,-name))
# remove id column
print(select(data1,-id))
Output:

Remove multiple columns by using column name
We can remove a column with select() method by its column name
Syntax:
select(dataframe,-c(column_name1,column_name2,.,column_name n)
Where, dataframe is the input dataframe and -c(column_names) is the collection of names of the column to be removed.
Example: R program to remove multiple columns
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
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'))
# remove name and id column
print(select(data1,-c(id,name)))
# remove name and address column
print(select(data1,-c(address,name)))
# remove all column
print(select(data1,-c(address,name,id)))
Output:

Remove a column by using column index
We can remove a column with select() method by its column index/position. Index starts with 1.
Syntax:
select(dataframe,-column_index)
Where, dataframe is the input dataframe and column_index is the position of the column to be removed.
Example: R program to remove particular column
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
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'))
# remove name column by its position
print(select(data1,-2))
# remove address column by its position
print(select(data1,-3))
Output:

Remove multiple columns by using column index
We can remove a column with select() method by its column index/position. Index starts with 1.
Syntax:
select(dataframe,-c(column_index1,column_index2,.,column_index n)
Where, dataframe is the input dataframe and c(column_indexes) is the position of the columns to be removed.
Example: R program to remove multiple columns by position
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
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'))
# remove name and id columns by
# its position
print(select(data1,-c(1,2)))
Output:
Similar Reads
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
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
2 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
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
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
How Do You Delete a Column by Name in data.table in R? Data manipulation is a critical aspect of the data analysis and R's data.table package is a powerful tool for handling large datasets efficiently. One common task is deleting a column by its name. This article will guide us through the process providing examples and best practices to ensure we can m
3 min read