mat1 <- cbind(mat, ave(mat[, 2], mat[, 3], FUN = sum))
#Changing 4th column to 1 for NA values in column 3.
mat1[is.na(mat[, 3]), 4] <- 1
mat1
# [,1] [,2] [,3] [,4]
# [1,] 1 0.11651699 1 0.15501901
# [2,] 1 0.03850202 1 0.15501901
# [3,] 0 0.11651699 NA 1.00000000
# [4,] 0 0.11651699 NA 1.00000000
# [5,] 1 0.04110752 39 0.07710048
# [6,] 1 0.03599296 39 0.07710048
# [7,] 1 0.05440237 41 0.09290439
# [8,] 1 0.11651699 42 0.17950417
# [9,] 1 0.06298718 42 0.17950417
#[10,] 0 0.11651699 NA 1.00000000
#[11,] 0 0.11651699 NA 1.00000000
#[12,] 0 0.03850202 41 0.09290439