Unexpected apply function behaviour in R
I've discovered a surprising behaviour by apply that I wonder if anyone can explain. Lets take a simple matrix: > (m = matrix(1:8,ncol=4)) [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 We can flip it vertically thus: > apply(m, MARGIN=2, rev) [,1] [,2] [,3] [,4] [1,] 2 4 6 8 [2,] 1 3 5 7 This applies the rev() vector reversal function iteratively to each column. But when we try to apply rev by row we get: > apply(m, MARGIN=1, rev) [,1] [,2] [1,] 7 8 [2,] 5 6 [3,] 3 4 [4,] 1 2 .. a 90 degree anti-clockwise rotation! Apply delivers the same result using FUN=function(v) {v[length(v):1]} so it is