How To Remove Multiple Rows In R (With Examples) - - Statology
Maybe your like
You can use one of the following methods to remove multiple rows from a data frame in R:
Method 1: Remove Specific Rows
#remove rows 2, 3, and 4 new_df <- df[-c(2, 3, 4), ]Method 2: Remove Range of Rows
#remove rows 2 through 5 new_df <- df[-c(2:5), ]Method 3: Remove Last N Rows
#remove rows 4 through last row new_df <- df[-c(4:nrow(df)), ]The following examples show how to use each of these methods in practice with the following data frame:
#create data frame df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'), points=c(99, 90, 86, 88, 95, 99), assists=c(33, 28, 31, 39, 34, 24)) #view data frame df team points assists 1 A 99 33 2 B 90 28 3 C 86 31 4 D 88 39 5 E 95 34 6 F 99 24Example 1: Remove Specific Rows
The following code shows how to remove rows 2, 3, and 4 from the data frame:
#define new data frame with rows 2, 3, 4 removed new_df <- df[-c(2, 3, 4),] #view new data frame new_df team points assists 1 A 99 33 5 E 95 34 6 F 99 24Notice that rows 2, 3, and 4 have all been removed from the data frame.
Example 2: Remove Range of Rows
The following code shows how to remove rows in the range of 2 through 5:
#define new data frame with rows 2 through 5 removed new_df <- df[-c(2:5),] #view new data frame new_df team points assists 1 A 99 33 6 F 99 24Notice that rows 2, 3, 4, and 5 have been removed.
Example 3: Remove Last N Rows
The following code shows how to remove rows 4 through the last row:
#remove rows 4 through last row new_df <- df[-c(4:nrow(df)), ] #view new data frame new_df team points assists 1 A 99 33 2 B 90 28 3 C 86 31Notice that row 4 and all rows after it have been removed.
Additional Resources
The following tutorials explain how to perform other common operations in R:
How to Remove Duplicate Rows in R How to Count Number of Rows in R How to Remove Rows with Some or All NAs in R
Tag » How To Remove Rows In R
-
How Do I Delete Rows In A Data Frame? - Stack Overflow
-
How To Remove Rows From An R Data Frame - With Examples
-
Delete Or Drop Rows In R With Conditions - DataScience Made Simple
-
How To Delete Rows In R? Explained With Examples
-
Remove Rows From The Data Frame In R - R-bloggers
-
R Data Frame - Delete Row Or Multiple Rows - Tutorial Kart
-
How To Remove/Delete A Row In R - Rows With NA, Conditions ...
-
How To Remove Single Row Or Multiple Rows In R - R-Lang
-
How To Remove Rows In R DataFrame? - GeeksforGeeks
-
Conditionally Remove Row From Data Frame In R (3 Examples)
-
Remove Rows With NA In R Data Frame (6 Examples) | Some Or All ...
-
How To Conditionally Remove Rows In R DataFrame?
-
How To Remove Rows And Columns Of A Matrix In R
-
How To Remove Rows In R Data Frame That Contains A Specific Number?