Similarly to the example on this page, I wan to sum the values in a column and divide it by the count of another. For example in the following code:
A <- c(1,1,2,2)
B <- c(0.2, 0.3, 1, 0.5)
M <- data.frame(A,B)
M
A B
1 1 0.2
2 1 0.3
3 2 1.0
4 2 0.5
I need to sum(B) with ID 1 and divide it by count(A) with ID 1. Similarly for ID 2.
The result should be as follows:
A.1 <- c(1, 2)
B.1 <- c((0.2+0.3)/2, (1+0.5)/2)
M.1 <- cbind(A.1, B.1)
M.1
A.1 B.1
[1,] 1 0.25
[2,] 2 0.75
Any ideas on how to go about doing that?