How to calculate means from igual intervals along my data

Hi,
I have a set of data of fish speed for every second during 3.5 hours (12600 rows), and I would like to extract mean speeds for every minute (total of 210 mean values). Which function should I use on r that I can avoid repeating the mean function 210 times?

I am new to R so I am kinda struggling with this.

Thank you

suppressPackageStartupMessages({library(dplyr)
                               library(data.table)})
# simulate a vector of 12,600 fish speed observations between 1 and 10
digits <- seq(1,10,1)
observations <- data.frame("fish" = sample(digits,12600, replace = TRUE))
setDT(observations)
n <- 60
observations[, mean(fish), by= (seq(nrow(observations)) - 1) %/% n]
#>      seq       V1
#>   1:   0 5.400000
#>   2:   1 5.016667
#>   3:   2 5.150000
#>   4:   3 5.450000
#>   5:   4 5.700000
#>  ---             
#> 206: 205 5.666667
#> 207: 206 5.933333
#> 208: 207 4.983333
#> 209: 208 5.816667
#> 210: 209 4.833333

Created on 2020-09-09 by the reprex package (v0.3.0)

1 Like

Thank you so much!!!

1 Like

This topic was automatically closed 21 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.