In this tutorial, we will learn how to delete or drop a column or multiple columns from a dataframe in R programming with examples.

You cannot actually delete a column, but you can access a dataframe without some columns specified by negative index. This is also called subsetting in R programming.

To delete a column, provide the column number as index to the Dataframe. The syntax is shown below:

where

  • mydataframe is the dataframe.
  • column_index_1, column_index_2, . . . are the comma separated indices which should be removed in the resulting dataframe.

Example: Drop Column from Dataframe

Let us create a dataframe, DF1

Let us assume that we need DF1 with V2 column deleted. The index of V2 column is 2. Now, we will access this dataframe with a negative index and store the result in another Dataframe DF2.

Viola. We have created a new dataframe with a column deleted from the previous dataframe.

Example: Delete Multiple Columns from Dataframe

Let us create a dataframe, DF1

Let us assume that we need DF1 with V2 and V3 deleted. The index of V2 is 2 and V3 is 3. Now, we will access this dataframe with a vector of negative indices and store the result in another Dataframe DF2.

Viola. We have created a new dataframe with multiple columns deleted from the previous dataframe.