Remove Rows from the data frame in R
Remove Rows from the data frame in R, To remove rows from a data frame in R using dplyr, use the following basic syntax.
Detecting and Dealing with Outliers: First Step – Data Science Tutorials
1. Remove any rows containing NA’s.
df %>% na.omit()
2. Remove any rows in which there are no NAs in a given column.
df %>% filter(!is.na(column_name))
3. Get rid of duplicates
df %>% distinct()
Sorting in r: sort, order & rank R Functions – Data Science Tutorials
4. Remove rows based on their index position.
df %>% filter(!row_number() %in% c(1, 2, 4))
5. Based on the condition, remove rows.
df %>% filter(column1=='A' | column2 > 8)
With the given data frame, the following examples explain how to apply each of these approaches in practice.
library(dplyr)
Now we can create a data frame.
Methods for Integrating R and Hadoop complete Guide – Data Science Tutorials
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))
Let’s view the data frame
df
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))
Approach 1: Remove Any Row with NA’s
The following code explains how to eliminate any rows from the data frame that have NA values.
delete any row that has the letter NA in it.
How to create contingency tables in R? – Data Science Tutorials
df %>% na.omit()
player points assists 1 P1 122 43 2 P2 144 55 3 P3 154 77 4 P4 155 18 5 P5 120 114 7 P7 229 29
Approach 2: Delete any rows that contain NAs in specific columns.
The following code demonstrates how to delete any row in a column containing NA values.
delete any rows in the ‘points’ column that have a NA.
How to Use the Multinomial Distribution in R? – Data Science Tutorials
df %>% filter(!is.na(assists))
player points assists 1 P1 122 43 2 P2 144 55 3 P3 154 77 4 P4 155 18 5 P5 120 114 6 P7 229 29
Approach 3: Rows that are duplicated should be removed.
The code below demonstrates how to eliminate duplicate rows.
duplicate rows should be removed
df %>% distinct()
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 4: Rows are removed based on their index position.
The code below demonstrates how to eliminate rows based on their index position.
Statistical test assumptions and requirements – Data Science Tutorials
Rows 1, 2, and 4 should be removed.
df %>% filter(!row_number() %in% c(1, 2, 4))
player points assists 1 P3 154 77 2 P5 120 114 3 P6 218 NA 4 P7 229 29
Approach 5: Rows are removed based on their condition.
The code below demonstrates how to eliminate rows based on certain criteria.
glm function in r-Generalized Linear Models – Data Science Tutorials
only keep rows when the team letter is ‘A’ or the number of points is more than eight.
df %>% filter(player=='P1' | assists >100)
player points assists 1 P1 122 43 2 P5 120 114