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:



How to Remove a Column by name and index using Dplyr Package in R

In this article, we are going to remove columns by name and index in the R programming language using dplyr package.

Dataset in use:

Similar Reads

Remove a column by using column name

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

Remove multiple columns by using column name

...

Remove a column by using column index

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

Remove multiple columns by using column index

...

Contact Us