Skip to content

Data Science Tutorials

For Data Science Learners

  • Top 7 Skills Required to Become a Data Scientist
    Top 7 Skills Required to Become a Data Scientist Machine Learning
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Best online course for R programming
    Best online course for R programming Course
  • Cross product of transpose of matrix in R R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • Bind together two data frames by their rows or columns in R
    Bind together two data frames by their rows or columns in R R
  • How to do Conditional Mutate in R
    How to do Conditional Mutate in R? R
Remove Rows from the data frame in R

Remove Rows from the data frame in R

Posted on June 3June 3 By Admin No Comments on 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

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Arrange the rows in a specific sequence in R
Next Post: How to Remove Columns from a data frame in R

Related Posts

  • Predict potential customer in R
    Predict potential customers in R R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • Group By Minimum in R
    Group By Minimum in R R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • How to Display Percentages on Histogram IN R
    How to Display Percentages on Histogram in R R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Extract values from vector in R: dplyr R
  • Best online course for R programming
    Best online course for R programming Course
  • How Do Machine Learning Chatbots Work
    How Do Machine Learning Chatbots Work Machine Learning
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • stacked barplot in R
    Stacked Barplot in R R
  • Multiple Plots to PDF in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme