How can I average every 2n element of a list?

2790 views r
2

I have a list of 86 values, where each two listed are results of one sample. I am measuring 43 samples.

Eg (abbrv)

   5.157
4.266
5.118
5.473
4.909
5.093

I am trying to get this end result

4.7115
5.2955
5.001

answered question

1 Answer

0

We create a grouping variable with gl and use group by mean

unname(tapply(v1,  as.integer(gl(length(v1), 2, length(v1))), FUN = mean))
#[1] 4.7115 5.2955 5.0010

data

v1 <- c(5.157, 4.266, 5.118, 5.473, 4.909, 5.093)

posted this

Have an answer?

JD

Please login first before posting an answer.