How To Remove Columns In R (With Examples) - - Statology
Maybe your like
Often you may want to remove one or more columns from a data frame in R. Fortunately this is easy to do using the select() function from the dplyr package.
library(dplyr)This tutorial shows several examples of how to use this function in practice using the following data frame:
#create data frame df <- data.frame(player = c('a', 'b', 'c', 'd', 'e'), position = c('G', 'F', 'F', 'G', 'G'), points = c(12, 15, 19, 22, 32), rebounds = c(5, 7, 7, 12, 11)) #view data frame df player position points rebounds 1 a G 12 5 2 b F 15 7 3 c F 19 7 4 d G 22 12 5 e G 32 11Example 1: Remove Columns by Name
The following code shows how to remove columns from a data frame by name:
#remove column named 'points' df %>% select(-points) player position rebounds 1 a G 5 2 b F 7 3 c F 7 4 d G 12 5 e G 11Example 2: Remove Columns in List
The following code shows how to remove columns from a data frame that are in a specific list:
#remove columns named 'points' or 'rebounds' df %>% select(-one_of('points', 'rebounds')) player position 1 a G 2 b F 3 c F 4 d G 5 e GExample 3: Remove Columns in Range
The following code shows how to remove all columns in the range from ‘position’ to ‘rebounds’:
#remove columns in range from 'position' to 'rebounds' df %>% select(-(position:rebounds)) player 1 a 2 b 3 c 4 d 5 eExample 4: Remove Columns that Contain a Phrase
The following code shows how to remove all columns that contain the word ‘points’
#remove columns that contain the word 'points' df %>% select(-contains('points')) player position rebounds 1 a G 5 2 b F 7 3 c F 7 4 d G 12 5 e G 11Example 5: Remove Columns that Start with Certain Letters
The following code shows how to remove all columns that start with the letters ‘po’:
#remove columns that start with 'po' df %>% select(-starts_with('po')) player rebounds 1 a 5 2 b 7 3 c 7 4 d 12 5 e 11Example 6: Remove Columns that End with Certain Letters
The following code shows how to remove all columns that end with the letter ‘s’:
#remove columns that end with 's' df %>% select(-ends_with('s')) player position 1 a G 2 b F 3 c F 4 d G 5 e GExample 7: Remove Columns by Position
The following code shows how to remove columns in specific positions:
#remove columns in position 1 and 4 df %>% select(-1, -4) position points 1 G 12 2 F 15 3 F 19 4 G 22 5 G 32Note: You can find the complete documentation for the select() function here.
Additional Resources
The following tutorials explain how to perform other common operations using dplyr:
How to Remove Rows Using dplyr How to Remove Rows with NA Values Using dplyr How to Select Columns by Index Using dplyr
Tag » How To Remove Columns In R
-
R : Keep / Drop Columns From Data Frame - ListenData
-
Drop Data Frame Columns By Name - Stack Overflow
-
How To Remove Columns From A Data Frame In R - R-bloggers
-
How To Remove A Column From An R Data Frame? - Tutorialspoint
-
How To Remove Column In R? - Spark By {Examples}
-
How To Drop Columns By Name In R? - Spark By {Examples}
-
How To Remove A Column In R Using Dplyr (by Name And Index)
-
Examples Of How To Add And Delete Columns From An R Dataframe
-
Drop Column In R Using Dplyr - Drop Variables
-
3 Ways To Remove Columns By Name In R
-
Remove Columns In R - Linux Hint
-
Drop Column(s) By Name From A Given DataFrame In R
-
Remove All-NA Columns From Data Frame In R (Example)
-
Remove Data Frame Columns By Name In R (6 Examples)