In Python, if a
is a Numpy array, I can do:
a[..., 0]
To get the first element of the last dimension, no matter the shape
of a
.
Example:
a = np.array([1,2,3])
a[..., 0]
Out[128]: array(1)
a = np.array([[1,2,3],[4,5,6]])
a[..., 0]
Out[130]: array([1, 4])
a = np.array([[[1,2,3],[4,5,6]], [[7,8,9],[10,11,12]]])
a[..., 0]
Out[132]:
array([[ 1, 4],
[ 7, 10]])
Didn't find an equivalent in base R here.
Is there a way to do this in (base) R to any array? Can really be helpful in generic functions which receive any nD array.