Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The post How to Remove Columns from a data frame in R appeared first on Data Science Tutorials
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
The post How to Remove Columns from a data frame in R appeared first on Data Science Tutorials
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.