mat1 <- matrix(1:12, nrow = 3)
mat1
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
apply(X = mat1, MARGIN = 1, FUN = max)
# [1] 10 11 12
c( max(mat1[1, ]), max(mat1[2, ]), max(mat1[3, ]) )
# [1] 10 11 12
apply(X = mat1, MARGIN = 2, FUN = max)
# [1] 3 6 9 12
c( max(mat1[, 1]), max(mat1[, 2]), max(mat1[, 3]), max(mat1[, 4]) )
# [1] 3 6 9 12
udf_add_n <- function(x, n = 0) {
x + n
}
apply(mat1, 1, udf_add_n, 3)
# [,1] [,2] [,3]
# [1,] 4 5 6
# [2,] 7 8 9
# [3,] 10 11 12
# [4,] 13 14 15
arr1 <- array(1:24, dim = c(3,4,2))
apply(arr1, 1, max)
# [1] 22 23 24
apply(arr1, 2, max)
# [1] 15 18 21 24
apply(arr1, 3, max)
# [1] 12 24
.EOF.