0

I'm new to R.

I'm trying to calculate the mean and variance of a vector.

However rather than simply calculating the mean and variance of the whole vector, I am trying to calculate the mean and variance of each "block of 10 elements".

For example

v <- c(1:100)
mean(v) # returns 50.5
var(v) # returns 841.6667

what I actually want is a new vector, with length 10, containing this:

mean(v[1:10]), mean(v[11:20]), ... , mean(v[91:100])

(and also the variance)

var(v[1:10]), var(v[11:20]), ... , var(v[91:100])

However I don't know how to accomplish this in a sensible way. Any help appreciated even if that is "go read this reference at [url]". I tried duckduckgo searching for this, but to no avail.

Seems like something that should be easy?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • Several alternatives here: [Calculate the mean of every 13 rows in data frame](https://stackoverflow.com/questions/30359427/calculate-the-mean-of-every-13-rows-in-data-frame). Replace the `nrow` (of the data frame) with `length` (of your vector) – Henrik Mar 12 '21 at 23:32

1 Answers1

1

Create a grouping variable that increments 1 every 10 values

grp <- as.integer(gl(length(v), 10, length(v)))

and then use a group by operation

tapply(v, grp, mean)
tapply(v, grp, var)

Or this can be done in dplyr

library(dplyr)
tibble(v) %>%
    group_by(grp = as.integer(gl(n(), 10, n()))) %>%
    summarise(Mean = mean(v), Var = var(v), .groups = 'drop')
akrun
  • 874,273
  • 37
  • 540
  • 662