Drop Columns of R DataFrame

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:

</>
Copy
 mydataframe[-c(column_index_1, column_index_2)]

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 1 – Drop Column from Dataframe

Let us create a dataframe, DF1

</>
Copy
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16))
> DF1
  V1 V2 V3
1  1  9  9
2  5 15  7
3 14 85 42
4 23  3 87
5 54 42 16
>

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.

</>
Copy
> DF2 = DF1[-2]
> DF2
  V1 V3
1  1  9
2  5  7
3 14 42
4 23 87
5 54 16
>

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

Example 2 – Delete Multiple Columns from DataFrame

Let us create a dataframe, DF1

</>
Copy
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16), V4= c(17, 25, 14, 23, 54), V5= c(9, 15, 85, 43, 2), V6= c(9, 75, 4, 7, 6))
> DF1
  V1 V2 V3 V4 V5 V6
1  1  9  9 17  9  9
2  5 15  7 25 15 75
3 14 85 42 14 85  4
4 23  3 87 23 43  7
5 54 42 16 54  2  6
>

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.

</>
Copy
> DF2 = DF1[c(-2,-3)]
> DF2
  V1 V4 V5 V6
1  1 17  9  9
2  5 25 15 75
3 14 14 85  4
4 23 23 43  7
5 54 54  2  6
> 

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

Conclusion

In this R Tutorial, we have learnt how to delete or drop a column or multiple columns from an R DataFrame.