In this tutorial, we’re going to show how to remove or delete a column of a data frame in R.
1. Preparation
Let’s create a data frame which will be used for all examples in this tutorial:
1 2 3 4 5 6 7 |
name <- c("mary", "al", "bea", "carol") age <- c(5, 6, 7, 4) hair <- c("blond","brown", "black", "blond") eye <- c("blue" ,"brown", "green", "black") # Create data frame children <- data.frame(name, age, hair, eye) |
And let’s see it:
1 2 3 4 5 6 |
> children name age hair eye 1 mary 5 blond blue 2 al 6 brown brown 3 bea 7 black green 4 carol 4 blond black |
2. Delete A Column Of A Data Frame In R Directly
To remove or delete a column of a data frame, we can set that column to NULL which is a reserved word and represents the null object in R. For example, let’s delete the column “hair” of the above data frame:
1 |
children$hair <- NULL |
And let’s see the data frame content:
1 2 3 4 5 6 |
> children name age eye 1 mary 5 blue 2 al 6 brown 3 bea 7 green 4 carol 4 black |
We can see that the column “hair” was deleted from the data frame.
Because there are other different ways to select a column of a data frame in R, we can have different ways to remove or delete a column of a data frame in R, for example:
To delete the column “hair” from the original data frame:
1 |
children[3] <- NULL |
Or
1 |
children[[3]] <- NULL |
We can achieve the same result with the first way. Let’s see the data frame again:
1 2 3 4 5 6 |
> children name age eye 1 mary 5 blue 2 al 6 brown 3 bea 7 green 4 carol 4 black |
3. Delete A Column Of A Data Frame By Subsetting
Another approach for us to remove or delete a column of a data frame in R is to leveraging the subsetting of a data frame, for example, to delete the column “eye” which is the 4th column of the above data frame:
1 |
children <- children[, -4] |
or
1 |
children <- children[-4] |
and see the data frame content:
1 2 3 4 5 6 |
> children name age hair 1 mary 5 blond 2 al 6 brown 3 bea 7 black 4 carol 4 blond |
The 4th column was deleted.
Notice that the [, -4] or [-4] mean to drop the 4th column from the data frame “children”(or take all columns but the 4th from the data frame “children”).
4. Delete Multiple Columns Of A Data Frame
4.1. Delete Multiple Columns By Index
In similar to deleting a column of a data frame, to delete multiple columns of a data frame, we simply need to put all desired column into a vector and set them to NULL, for example, to delete the 2nd, 4th columns of the above data frame:
1 |
children[c(2, 4)] <- list(NULL) |
Let’s have a look at the result:
1 2 3 4 5 6 |
> children name hair 1 mary blond 2 al brown 3 bea black 4 carol blond |
To drop the columns 2, 3, 4 of the data frame:
1 |
children[2:4] <- list(NULL) |
And the result:
1 2 3 4 5 6 |
> children name 1 mary 2 al 3 bea 4 carol |
4.2. Delete Multiple Columns By Names
To remove or delete multiple columns of a data frame by names, we need to provides the list of names when subsetting, for example, to delete the columns “hair” and “eye” of the above data frame:
1 2 |
drops <- c("hair","eye") children[ , !(names(children) %in% drops)] |
The result:
1 2 3 4 5 6 |
> children[ , !(names(children) %in% drops)] name age 1 mary 5 2 al 6 3 bea 7 4 carol 4 |
Or, alternatively, we make a list of columns need to keep and refer to them by name:
1 2 |
keeps <- c("name", "age") children[keeps] |
The result is identical to the above example:
1 2 3 4 5 6 |
> children[keeps] name age 1 mary 5 2 al 6 3 bea 7 4 carol 4 |
5. Conclusion
The tutorial has shown us how to remove or delete a column of a data frame in R by two approaches. The first one is to remove or delete the column directly, and the second one is to through subsetting the data frame.
Below are other related tutorials for your references:
- R – Rename Column of Data Frame
- Read CSV File in R
- Write CSV File in R
- R – Add New Column To A Data Frame
- R – How To Order A Data Frame