Katie_C94
Katie_C94

Reputation: 3

Average of n values in column in R - repeat

I have created a data frame from data x:

  d1 <- data.frame(x$length, x$weight, x$volume)

And a second data frame:

  d2 <- data.frame(
    mean1 <- mean(d1$x.volume[1:5]),
    mean2 <- mean(d1$x.volume[6:10]),
    mean3 <- mean(d1$x.volume[11:15]),
    mean4 <- mean(d1$x.volume[16:20]))

So I am trying to find an average for the first 5 rows, the next 5 etc. I have to do this for hundreds of rows. Is there a more simple way of doing it?

Upvotes: 0

Views: 217

Answers (2)

IRTFM
IRTFM

Reputation: 263311

The zoo library has several functions for handling sequential operations. This one can be handled with rollapply using 5 as the by= parameter:

library(zoo)

set.seed(42)
 n <- 1000
 d1 <- data.frame(x.volume = rnorm(n))
 d1.means.by5 <- rollapply(d1, width=5, FUN=mean, by=5)
 str(d1.means.by5)
num [1:200, 1] 0.441 0.653 0.358 -0.685 0.17 ...
- attr(*, "dimnames")=List of 2
 ..$ : NULL
 ..$ : chr "x.volume"

I first looked at the help page for rollmean, but it didn't offer the same level of control about the stepping forward by 5.

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

First, make up a dataset, since you have not posted one.

set.seed(1)
n <- 100
d1 <- data.frame(x.volume = rnorm(n))

Now, you can aggregate on a variable that has 5 equal elements in a row. A cumsum trick will create such a variable.

by <- 5
fac <- c(1, rep(0, times = by - 1))
fac <- cumsum(rep(fac, length.out = n))

agg <- aggregate(x.volume ~ fac, d1, mean)
head(agg)
#  fac    x.volume
#1   1  0.12926990
#2   2  0.13513567
#3   3  0.03812297
#4   4  0.45956697
#5   5  0.08123054
#6   6 -0.34857703

Upvotes: 1

Related Questions