How to Remove Columns from a data frame in R
Remove Columns from a data frame, you may occasionally need to remove one or more columns from a data frame. Fortunately, the select() method from the dplyr package makes this simple.
Remove Rows from the data frame in R – Data Science Tutorials
library(dplyr)
Using the data frame below, this tutorial demonstrates numerous examples of how to utilize this function in practice.
Remove Columns from a data frame
Let’s create a data frame
df <- data.frame(player = c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'),
points = c(122, 144, 154, 155, 120, 218, 229),
assists = c(43, 55, 77, 18, 114, NA,29))Now we can view the data frame
One way ANOVA Example in R-Quick Guide – Data Science Tutorials
df
player points assists 1Â Â Â Â P1Â Â Â 122Â Â Â Â Â 43 2Â Â Â Â P2Â Â Â 144Â Â Â Â Â 55 3Â Â Â Â P3Â Â Â 154Â Â Â Â Â 77 4Â Â Â Â P4Â Â Â 155Â Â Â Â Â 18 5Â Â Â Â P5Â Â Â 120Â Â Â Â 114 6Â Â Â Â P6Â Â Â 218Â Â Â Â Â NA 7Â Â Â Â P7Â Â Â 229Â Â Â Â Â 29
Approach 1: Remove Columns by Name
How to delete columns from a data frame by name is demonstrated in the following code.
delete the ‘points’ column
How to perform the Kruskal-Wallis test in R? – Data Science Tutorials
df %>% select(-points)
player assists 1Â Â Â Â P1Â Â Â Â Â 43 2Â Â Â Â P2Â Â Â Â Â 55 3Â Â Â Â P3Â Â Â Â Â 77 4Â Â Â Â P4Â Â Â Â Â 18 5Â Â Â Â P5Â Â Â Â 114 6Â Â Â Â P6Â Â Â Â Â NA 7Â Â Â Â P7Â Â Â Â Â 29
Approach 2: Remove Columns in the List
The code below demonstrates how to delete columns from a data frame that belong to a certain list.
‘Points’ and ‘player’ columns should be removed.
5 Free Books to Learn Statistics For Data Science – Data Science Tutorials
df %>% select(-one_of('points', 'player'))assists 1Â Â Â Â Â 43 2Â Â Â Â Â 55 3Â Â Â Â Â 77 4Â Â Â Â Â 18 5Â Â Â Â 114 6Â Â Â Â Â NA 7Â Â Â Â Â 29
Approach 3: Remove Columns in Range
To remove all columns in the range from ‘position’ to ‘points,’ use the following code.
delete columns from ‘player’ to ‘points’ in the range.
df %>% select(-(player:points))
assists 1Â Â Â Â Â 43 2Â Â Â Â Â 55 3Â Â Â Â Â 77 4Â Â Â Â Â 18 5Â Â Â Â 114 6Â Â Â Â Â NA 7Â Â Â Â Â 29
Approach 4: Remove Columns that Contain a Phrase
The code below demonstrates how to delete all columns containing the word ‘points.’
glm function in r-Generalized Linear Models – Data Science Tutorials
delete columns with the word ‘points’ in them.
df %>% select(-contains('points'))player assists 1Â Â Â Â P1Â Â Â Â Â 43 2Â Â Â Â P2Â Â Â Â Â 55 3Â Â Â Â P3Â Â Â Â Â 77 4Â Â Â Â P4Â Â Â Â Â 18 5Â Â Â Â P5Â Â Â Â 114 6Â Â Â Â P6Â Â Â Â Â NA 7Â Â Â Â P7Â Â Â Â Â 29
Approach 5: Remove Columns that Start with Certain Letters
To eliminate all columns that begin with the letters ‘po,’ use the following code.
Hypothesis Testing Examples-Quick Overview – Data Science Tutorials
delete columns that begin with the letter ‘po’
df %>% select(-starts_with('po'))player assists 1Â Â Â Â P1Â Â Â Â Â 43 2Â Â Â Â P2Â Â Â Â Â 55 3Â Â Â Â P3Â Â Â Â Â 77 4Â Â Â Â P4Â Â Â Â Â 18 5Â Â Â Â P5Â Â Â Â 114 6Â Â Â Â P6 Â Â Â Â Â NA 7Â Â Â Â P7Â Â Â Â Â 29
Approach 6: Remove Columns that End with Certain Letters
To eliminate all columns that finish in the letter’s,’ use the following code:
‘s’-ending columns should be removed.
How to perform the MANOVA test in R? – Data Science Tutorials
df %>% select(-ends_with('s'))player 1Â Â Â Â P1 2Â Â Â Â P2 3Â Â Â Â P3 4Â Â Â Â P4 5Â Â Â Â P5 6Â Â Â Â P6 7Â Â Â Â P7
Approach 7: Remove Columns by Position
The code below demonstrates how to remove columns from certain locations:
Columns 1 and 3 should be removed.
df %>% select(-1, -3)
points 1Â Â Â 122 2Â Â Â 144 3Â Â Â 154 4Â Â Â 155 5Â Â Â 120 6Â Â Â 218 7Â Â Â 229