Molly
Molly

Reputation: 15

Get the average column values for a set of rows using R

I am trying to get the mean value of a column over a certain range of rows. For example, suppose:

data.frame(x=c(.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))

I want the average value of Rows 1-3, 4-6, and 7-9 in a new dataframe that looks like:

data.frame(x=c(0.2, 0.5, 0.8)

However, I have over 15,000 rows and want to take the average of 20 row at a time for each column. Is there a good way of doing this? I think a doing something with a subset of colMeans() is best, but I'm very novice at R.

Thank you!

Upvotes: 0

Views: 71

Answers (1)

Gautam
Gautam

Reputation: 2753

Here's a way using .colMeans() which gives the output you want.

dat <- data.frame(x=c(.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))
> .colMeans(x = dat$x, m = 3, n = nrow(dat)/3, na.rm = T )
[1] 0.2 0.5 0.8

testing on a larger data.frame:

> dat <- data.frame(x = rnorm(15000))
> 
> # Start timer
> tic()
> w <- .colMeans(x = dat$x, m = 3, n = nrow(dat)/3, na.rm = T )
> 
> # End timer
> toc()
0.01 sec elapsed
> 
> print(length(w))
[1] 5000

This is a bit faster than the method proposed by d.b:

> tic()
> w2 <- aggregate(. ~ grp, transform(dat, grp = ceiling(seq_along(x)/3)), mean)
> toc()
0.1 sec elapsed

Upvotes: 1

Related Questions