I have a data frame (wc2) with 7 columns:
cm5 cm10 cm15 cm20 cm25 cm30 run_time
1 0.1221060 0.1221060 0.1221060 0.1221060 0.1221060 0.1221060 0
2 0.4084525 0.4028010 0.3617393 0.2595060 0.1294412 0.1220099 2
3 0.4087809 0.4042515 0.3711077 0.3119956 0.2241836 0.1290348 4
4 0.4088547 0.4045780 0.3732053 0.3218224 0.2611785 0.1720426 6
5 0.4088770 0.4046887 0.3739936 0.3255557 0.2739738 0.2081264 8
6 0.4088953 0.4047649 0.3744183 0.3273794 0.2798225 0.2273250 10
For every row (run_time) I want to average first the 1st column, then the 1st and 2nd columns, then the 1st, 2nd and 3rd columns and so on until the 6th column. The averaged result should be in a new column or a new data frame (I don't mind). I did it using the following code:
wc2$dia10 <- wc2$cm5
wc2$dia20 <- rowMeans(wc2[c("cm5", "cm10")])
wc2$dia30 <- rowMeans(wc2[c("cm5", "cm10", "cm15")])
wc2$dia40 <- rowMeans(wc2[c("cm5", "cm10", "cm15", "cm20")])
wc2$dia50 <- rowMeans(wc2[c("cm5", "cm10", "cm15", "cm20", "cm25")])
wc2$dia60 <- rowMeans(wc2[c("cm5", "cm10", "cm15", "cm20", "cm25", "cm30")])
From my basic knowledge of R I gather there is a much better way of doing that but I can't figure out how. Especially for when I'll have a bigger number of columns. I had a look at the answer for "Sum over and increasing number of columns of a data frame in R" but couldn't understand or apply it to my data.
Thanks for any help