matrix와 array
a = matrix(1:20, nrow=5)
a
## [,1] [,2] [,3] [,4]
## [1,] 1 6 11 16
## [2,] 2 7 12 17
## [3,] 3 8 13 18
## [4,] 4 9 14 19
## [5,] 5 10 15 20
apply(a, 1, mean)
## [1] 8.5 9.5 10.5 11.5 12.5
apply(a, 2, mean)
## [1] 3 8 13 18
x = matrix(rnorm(20, mean=0, sd=10), nrow=4)
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] -9.934007 0.006597635 -3.84921296 6.084250 6.165411
## [2,] -20.017956 1.991361506 -7.30670901 5.212370 -9.884440
## [3,] -5.166649 -9.524993094 -0.08221195 3.766401 12.265300
## [4,] 6.278801 -2.190847659 -15.43569788 11.086988 4.038638
x1 = sweep(x, 1, apply(x, 1, min), `-`)
x1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000000 9.940604 6.084794 16.01826 16.09942
## [2,] 0.000000 22.009317 12.711247 25.23033 10.13352
## [3,] 4.358344 0.000000 9.442781 13.29139 21.79029
## [4,] 21.714499 13.244850 0.000000 26.52269 19.47434
x2 = sweep(x1, 1, apply(x, 1, max), `/`)
x2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 1.612318 0.9869242 2.598084 2.611248
## [2,] 0.0000000 4.222517 2.4386695 4.840471 1.944128
## [3,] 0.3553394 0.000000 0.7698777 1.083658 1.776581
## [4,] 1.9585570 1.194630 0.0000000 2.392235 1.756504
outer(1:3, 1:10, "*")
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 2 4 6 8 10 12 14 16 18 20
## [3,] 3 6 9 12 15 18 21 24 27 30