Open In App

How to Delete Multiple Columns in R DataFrame?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to delete multiple columns in R Programming Language. We can delete multiple columns in the R dataframe by assigning null values through the list() function.

Syntax:

data[ , c(‘column_name1’, ‘column_name2’,………..,’column_nam en)] <- list(NULL)

where, data is the input dataframe

Example: R program to create a dataframe and assign columns to null.

R




# dataframe
data = data.frame(column1=c(70, 76, 89),
                  column2=c(90, 79, 100),
                  column3=c(23, 4, 56),
                  column4=c(23, 45, 21))
  
  
# display
print(data)
  
# delete three columns
print(data[, c('column1', 'column2', 'column3')] < - list(NULL))


Output:

We can also delete columns using index of columns.

Syntax:

dataframe[ , column_index_start:column_index_end] <- list(NULL)

Where , 

  • dataframe is the input dataframe
  • column_index are the column positions

Example:

R




# dataframe
data = data.frame(column1=c(70, 76, 89),
                  column2=c(90, 79, 100),
                  column3=c(23, 4, 56),
                  column4=c(23, 45, 21))
  
  
# display
print(data)
  
# delete three columns
print(data[, 1:3] < - list(NULL))


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads