How do I make a data frame that consists of every third row of the iris dataset?

How do I make a data frame that consists of every third row of the iris dataset? I honestly do not even know where to start

Check if the row number is evenly divisible by 3 . The modulo operator %% returns the remainder, which is zero for 3, 6, 9, ... . In the tidyverse, use filter(row_number()%%3 == 0).

@EconProf 's solution is simple and elegant. This gives you the desired data.

library(tidyverse)

iris <- iris

iris_third  <-  iris %>% 
  filter(row_number() %% 3 == 0)

Created on 2021-10-11 by the reprex package (v2.0.1)

Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!

and for the base R version:

iris[1:nrow(iris) %% 3 ==  0, ]
1 Like

Or iris %>% slice(seq(1,nrow(iris),3))

1 Like