Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • Arrange Data by Month in R
    Arrange Data by Month in R with example R
  • Interactive 3d plot in R
    Interactive 3d plot in R-Quick Guide R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples 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

  • How do confidence intervals work
    How do confidence intervals work? R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection 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
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • Best online course for R programming
    Best online course for R programming Course
  • How to Use Gather Function in R
    How to Use Gather Function in R?-tidyr Part2 R
  • best books about data analytics
    Best Books to Learn Statistics for Data Science Course
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme