Drop column(s) by name from a given DataFrame in R Last Updated : 26 Mar, 2021 Comments Improve Suggest changes Like Article Like Report 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 usedexpr: Condition for a subset Approach Create data frameSelect subset of the data to be removedPut a minus signAssign to initial data frameDisplay 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 frameSelect columns to be deletedApply negationAssign it to the initial data frameDisplay 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 moduleCreate data frameSelect column to be removedApply minus signAssign it to the initial data frameDisplay 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:- Comment More infoAdvertise with us Next Article Drop column(s) by name from a given DataFrame in R geetansh044 Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs 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 Sort a given DataFrame by multiple column(s) in R Sorting of data may be useful when working on a large data and data is un-arranged, so it is very helpful to sort data first before applying operations. In this article, we will learn how to sort given dataframes by multiple columns in R. Approach:Create data frameChoose any more number of columns m 2 min read Merge DataFrames by Column Names in R In this article, we are going to see how to merge Dataframe by Column Names using merge in R Programming Language. The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly 3 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 filter R DataFrame by values in a column? In R Programming Language, dataframe columns can be subjected to constraints, and produce smaller subsets. However, while the conditions are applied, the following properties are maintained : Rows are considered to be a subset of the input.Rows in the subset appear in the same order as the original 5 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 Select Only Numeric Columns from DataFrame in R In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric colu 2 min read Like