I've got a cvs file with 2 columns (see below Matrix 1 for example). I would like to create a program to average the second column of the matrix for all the duplicate numbers in the first column. So for instance in the matrix below there are two rows of "2" in the first column. Those rows would be averaged into one column of ((356+456)/2 = 406) etc. So the final matrix would like the Matrix 2 at the bottom. Any ideas on how to do this?
Matrix 1
mat1 <- structure(c(1, 2, 2, 3, 4, 4, 4, 5, 234, 356, 456, 745, 568,
998, 876, 895), .Dim = c(8L, 2L))
mat1
[,1] [,2]
[1,] 1 234
[2,] 2 356
[3,] 2 456
[4,] 3 745
[5,] 4 568
[6,] 4 998
[7,] 4 876
[8,] 5 895
Matrix 2
mat2 <- structure(c(1, 2, 3, 4, 5, 234, 406, 745, 814, 895), .Dim = c(5L, 2L))
mat2
[,1] [,2]
[1,] 1 234
[2,] 2 406
[3,] 3 745
[4,] 4 814
[5,] 5 895