How to average every 20 values in a dataset.

I have a data set consisting of over 18,000 points.

I need to average every 20 values. So, I need to average 1 through 20, then 21 through 40, and so on.

The data set is within a file. Help?

rollmean() and rollapply() from the zoo package is your friend here.

Try to take a look at this:

# Load libraries
library("tidyverse")

# Set dummy data
my_data <- tibble(points = rnorm(n = 18000))

# Create groups
my_data <- my_data %>% 
  mutate(group = rep(seq(from = 1, to = 18000 / 20), each = 20))

# Calculate grouped means
my_data %>% 
  group_by(group) %>% 
  summarise(mu = mean(points))

Hope it helps :slightly_smiling_face:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.