Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • Checking Missing Values in R
    Checking Missing Values in R R
  • Select the First Row by Group in R
    Select the First Row by Group in R R
  • Best Books on Data Science with Python
    Best Books on Data Science with Python Course
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • Dealing Missing values in R
    Dealing With Missing values in R R
What is the best way to filter by row number in R?

What is the best way to filter by row number in R?

Posted on June 15June 11 By Jim No Comments on What is the best way to filter by row number in R?
Tweet
Share
Share
Pin

What is the best way to filter by row number in R?, The slice function from the dplyr package can be used to filter a data frame by row number using the following methods.

How to Count Distinct Values in R – Data Science Tutorials

Method 1: Filter by Specific Row Numbers

df %>% slice(2, 3, 8)

Rows 2, 3, and 8 will be returned as a result of this.

Method 2: Filter by Range of Row Numbers

df %>% slice(2:5)

Rows 2 through 5 will be returned as a result of this.

With the following data frame in R, the following examples explain how to utilize each method in practice.

How to perform the MANOVA test in R? – Data Science Tutorials

Let’s create a data frame

df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(110, 120, 80, 16, 105, 185, 112, 112),
                 rebounds=c(18, 18, 14, 13, 12, 15, 17, 12))

Now we can view the data frame

df
    team points rebounds
1    A    110       18
2    B    120       18
3    C     80       14
4    D     16       13
5    E    105       12
6    F    185       15
7    G    112       17
8    H    112       12

Example 1: Filter by Specific Row Numbers

To filter for rows 2, 3, and 8, we may use the following code.

Statistical test assumptions and requirements – Data Science Tutorials

library(dplyr)

Let’s filter for only rows 2, 3, and 8

df %>% slice(2, 3, 8)
   team points rebounds
1    B    120       18
2    C     80       14
3    H    112       12

From the original data frame, only rows 2, 3, and 8 are returned.

Example 2: Filter by a row number range

To filter for rows between 2 and 5, we can use the following code.

Sorting in r: sort, order & rank R Functions – Data Science Tutorials

library(dplyr)

Now filter for rows between 2 and 5

df %>% slice(2:5)
   team points rebounds
1    B    120       18
2    C     80       14
3    D     16       13
4    E    105       12

Only rows 2 to 5 from the original data frame are returned.

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to Filter Rows In R?
Next Post: Filter Using Multiple Conditions in R

Related Posts

  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to plot categorical data in R
    Plot categorical data in R R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • How to Count Distinct Values in R
    How to Count Distinct Values 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
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE R
  • Hypothesis Testing Examples
    Hypothesis Testing Examples-Quick Overview Statistics
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme