Drop column(s) by name from a given DataFrame in R
Last Updated :
26 Mar, 2021
Dropping of columns from a data frame is simply used to remove the unwanted columns in the data frame. In this article, we will be discussing the two different approaches to drop columns by name from a given Data Frame in R.
The different approaches to drop columns by the name from a data frame is R language are discussed below
Method 1: Using subset()
This is one of the easiest approaches to drop columns is by using the subset() function with the '-' sign which indicates dropping variables. This function in R Language is used to create subsets of a Data frame and can also be used to drop columns from a data frame.
Syntax:
subset(df, expr)
Parameters:
- df: Data frame used
- expr: Condition for a subset
Approach
- Create data frame
- Select subset of the data to be removed
- Put a minus sign
- Assign to initial data frame
- Display data frame
Example:
R
gfg <- data.frame(a=c('i','ii','iii','iv','v'),
x=c('I','II','III','IV','V'),
y=c(1,2,3,4,5),
z=c('a','b','c','d','e'))
print('Original dataframe:-')
gfg
gfg = subset(gfg, select = -c(x,z) )
print('Modified dataframe:-')
gfg
Output:

Method 2: Using names()
In this method, we are creating a character vector named drop in which we are storing column names Later we are telling R to select all the variables except the column names specified in the vector drop. The '!' sign indicates negation.
The function names() in R Language are used to get or set the name of an Object. This function takes the object i.e. vector, matrix, or data frame as an argument along with the value that is to be assigned a name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named and returns all the column names.
Syntax:
names(x) <- value
Parameters:
- x: Object i.e. vector, matrix, data frame, etc.
- value: Names to be assigned to x
Approach
- Create data frame
- Select columns to be deleted
- Apply negation
- Assign it to the initial data frame
- Display data frame
Example:
R
gfg <- data.frame(a=c('i','ii','iii','iv','v'),
x=c('I','II','III','IV','V'),
y=c(1,2,3,4,5),
z=c('a','b','c','d','e'))
print('Original dataframe:-')
gfg
drop <- c("x","z")
gfg = gfg[,!(names(gfg) %in% drop)]
print('Modified dataframe:-')
gfg
Output:

Method 3: Using select()
In this approach, we will be using select() by the import the dplyr library in R language and specifying the parameter to drop the columns of the dataset. Basically, this function keeps only the variables you mention.
Syntax:-
select(.data, ...)
Parameters:-
- data:-A data frame, data frame extension, or a lazy data frame.
- ... :- One or more unquoted expressions separated by commas. Variable names can be used as if they were positions in the data frame, so expressions like x:y can be used to select a range of variables.
Approach
- Import module
- Create data frame
- Select column to be removed
- Apply minus sign
- Assign it to the initial data frame
- Display data frame
Example:-
R
library(dplyr)
gfg <- data.frame(a=c('i','ii','iii','iv','v'),
x=c('I','II','III','IV','V'),
y=c(1,2,3,4,5),
z=c('a','b','c','d','e'))
print('Original dataframe:-')
gfg
print('Modified dataframe:-')
select(gfg, -a)
Output:-
Similar Reads
Drop row(s) by number from given DataFrame in R In this article, we will be discussing the approaches to drop rows by a number from a given Data Frame in R language. Dropping of rows from a data frame is simply used to remove the unwanted rows in the data frame. Method 1: Using minus(-) sign In this method, the user needs to provide the index of
1 min read
Extract given rows and columns from a given dataframe in R Extraction of given rows and columns has always been one of the most important tasks which are especially required while working on data cleaning activities. In this article, we will be discussing all the sets of commands which are used to extract given rows and columns from a given dataframe in the
4 min read
Reorder DataFrame by column name in R It is very difficult any time taking task if we reorder the column name, so we use R Programming Language to do it effectively. In this article, we will be discussing the three different ways to reorder a given DataFrame by column name in R. Method 1: Manually selecting the new order of the column n
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
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
Extract specific column from a DataFrame using column name in R In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language. In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is,
5 min read