Given a data frame with two columns, I'm looking to calculate a third column which would contain the mean for every n number of rows while keeping the data frame intact.
Given the data frame
index<-1:20
V<-c(2,5,7,4,8,9,4,6,8,NA,3,4,5,6,0,4,5,7,5,3)
DF<-data.frame(index,V)
How could I create DF$mean which would be the non-rolling mean of every 5 rows.
index V mean
1 2 5.2
2 5 5.2
3 7 5.2
4 4 5.2
5 8 5.2
6 9 6.75
7 4 6.75
8 6 6.75
9 8 6.75
10 NA 6.75
11 3 3.6
12 4 3.6
13 5 3.6
14 6 3.6
15 0 3.6
16 4 4.8
17 5 4.8
18 7 4.8
19 5 4.8
20 3 4.8
DF$mean <- ave(DF$V,
rep(1:(nrow(DF)/5), each=5),
FUN=function(x){mean(x, na.rm=TRUE)})
which gives
> DF
index V mean
1 1 2 5.20
2 2 5 5.20
3 3 7 5.20
4 4 4 5.20
5 5 8 5.20
6 6 9 6.75
7 7 4 6.75
8 8 6 6.75
9 9 8 6.75
10 10 NA 6.75
11 11 3 3.60
12 12 4 3.60
13 13 5 3.60
14 14 6 3.60
15 15 0 3.60
16 16 4 4.80
17 17 5 4.80
18 18 7 4.80
19 19 5 4.80
20 20 3 4.80
You can use colMeans and rep
DF$mean <- rep(colMeans(matrix(DF$V, nrow=5), na.rm=TRUE), each=5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With