Drop column(s) by name from a given DataFrame in R - GeeksforGeeks

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
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:
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:
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:-
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R