How to group by a fixed number of rows in dplyr?

前端 未结 2 552
北海茫月
北海茫月 2021-01-12 21:11

I have a data frame:

set.seed(123)
x <- sample(10)
y <- x^2
my.df <- data.frame(x, y)

The result is this:

> my.         


        
2条回答
  •  [愿得一人]
    2021-01-12 21:55

    Another option could be:

    my.df %>%
     group_by(x = ceiling(row_number()/5)) %>%
     summarise_all(list(sum = sum, mean = mean))
    
          x   sum  mean
        
    1     1   174  34.8
    2     2   211  42.2
    

提交回复
热议问题