[R] Deleting the last value of a vector

Kenn Konstabel lebatsnok at gmail.com
Mon Apr 18 14:13:34 CEST 2011


On Mon, Apr 18, 2011 at 4:14 AM, helin_susam <helin.susam at gmail.com> wrote:
> if you have a vector like as follows;
>
> r=c(1,2,3,4,5)
>
> then use
>
> r2=r[1:length(r)-1]

Umm ... this works and gives the intended answer but does so in an ugly way --

1:length(r)-1 is equivalent to (1:length(r))-1 or 0:(length(r)-1) --
in other words, you get a sequence from 0 to 4. The result of r[0] is,
curiously, integer(0) (but if you think about it, it might just as
well be an error), so you get the intended result but what you
probably actually meant is

r2=r[1:(length(r)-1)]

# or

r2=r[1:seq_len(length(r)-1)]


KK



More information about the R-help mailing list