[R] Determining Index of Last Element in Vector

Alan Lue alan.lue at gmail.com
Sun Apr 25 20:19:06 CEST 2010


Sorry -- I meant `v(end)' and `v[length(v)]' in the first examples of
my message.

Alan


On Sun, Apr 25, 2010 at 11:10 AM, Alan Lue <alan.lue at gmail.com> wrote:
> Hi,
> Is there a way to specify the last element of a vector, similar to "end" in
> MATLAB?
>   v[end]
> would be MATLAB for
>   v(length(v))
> in R.
> While `v(length(v))' does yield the last element, that approach fails in the
> following,
>   rep(v, each=2)[-c(1,length(v))]
> which is meant to duplicate all elements of `v' except for the first and
> last.  (I.e., if `v <- 1:4', then we want '1 2 2 3 3 4'.)
> So the question is, is there a better way specify the last element of a
> vector?  If not, is there a better way to duplicate all elements of a vector
> except for the first and last?  (I know you can achieve this using two
> lines, but I'm writing because I want to do it using one.)
> Alan
>
> --
> Alan Lue
> Master of Financial Engineering
> UCLA Anderson School of Management
>



-- 
Alan Lue
Master of Financial Engineering
UCLA Anderson School of Management



More information about the R-help mailing list