I have a matrix with many columns that need to be combined separately. I want to combine them and take the means across rows and then put them into a new matrix. TYIA
I have a matrix called data like this:
p7.1 p7.2 p7.3 p8.1 p8.2 p9.1 p9.2
1 0 1 0 4 5 2 1
2 15 6 2 3 4 10 1
3 1 2 4 5 1 0 2
This is my desired result: All "p7" should be in one column with the value of the mean of the rows of all sep. p7 values.
p7 p8 p9
1 .5 4.5 1.5
2 11.5 3.5 5.5
3 3.5 3 1
I am able to execute this by saving p7, p8, p9 to different variables like this: p7 <- apply(data[,1:3], 1, mean) etc. However, I'm struggling to find a more concise way to do this. Perhaps a for loop, but I have not been able to successfully do this.