Suppose I have a vector v
, how do I get its reverse, i.e. last element first?
The first thing that comes to me is v[length(v):1]
, but it returns NA when v
is numeric(0)
, while user normally expect sorting nothing returns nothing, not sorting nothing returns the unavailable thing - it does make a big difference in my case.
v <- c()
:length(v):1
is0:1
. The[
operator ignores the0
and returnsv[1]
, i.e.NA
. If it were not forrev
, something robust and along the lines of the OP would have beenv[seq(to = 1, by = -1, length.out = length(v))]
. – Hurwit