Drop column name with Regular Expression

Here we are going to drop the column based on the pattern given in grepl() function. It will find a pattern and remove the column based on the given pattern

Syntax:

dataframe[,!grepl(“pattern”,names(dataframe))]

Here, dataframe is the input dataframe and pattern is the expression to remove the column.

Pattern to remove the column where starting character in column starts is

Syntax:

data[,!grepl(“^letter”,names(data))]

Example: R program to remove column that starts with a letter

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'))
  
  
# drop  column that starts with n
print(data1[,!grepl("^n",names(data1))])
      
# remove column that starts with a
print(data1[,!grepl("^a",names(data1))])


Output:



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:

Similar Reads

Drop multiple columns by using the column name

We can remove a column with select() method by its column name...

Drop multiple columns by using column index

...

Drop column which contains a value or matches a pattern

We can remove a column with select() method by its column index/position. Index starts with 1....

Remove column which starts with or ends with certain character

...

Drop column name with Regular Expression

Let’s see how to remove the column that contains the character/string....