I think your confusion stems from the fact that apply does not pass an array (or matrix) to the function specified in FUN .
It passes each row of the matrix in turn. Each line itself is a "only" (named) vector:
> m[1,] abc 0.48768161 0.61447934 0.08718875
So your function only has this named vector to work with.
For your average example, as described in apply :
If each FUN call returns a vector of length n, then apply a return array of dimension c (n, dim (X) [MARGIN]), if n> 1. If n is 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim (X) [MARGIN] otherwise.
So function(x) names(x) returns a vector of length 3 for each row, so the end result is the matrix you see. But this matrix is ββbuilt at the end of the apply function, and the FUN results are applied to each row separately.
joran source share