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
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.