Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • ggpairs in R
    ggpairs in R R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • Gamma distribution in R
    Gamma distribution in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • droplevels in R with examples
    droplevels in R with examples R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot 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 Jim No Comments on Remove Rows from the data frame in R
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • Interactive 3d plot in R
    Interactive 3d plot in R-Quick Guide R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • How to change the column positions in R?
    How to change the column positions in R? R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • How do confidence intervals work
    How do confidence intervals work? R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • Quantiles by Group calculation in R
    Quantiles by Group calculation in R with examples R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme