Re: [R] Deleting the last value of a vector

From: jim holtman <jholtman_at_gmail.com>
Date: Mon, 18 Apr 2011 08:21:10 -0400

Why not use 'head'

> head(1:10, -1)

[1] 1 2 3 4 5 6 7 8 9
> head(1, -1)

numeric(0)
> head(integer(0), -1)

integer(0)

On Mon, Apr 18, 2011 at 8:13 AM, Kenn Konstabel <lebatsnok_at_gmail.com> wrote:
> On Mon, Apr 18, 2011 at 4:14 AM, helin_susam <helin.susam@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
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 18 Apr 2011 - 12:28:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 13:10:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive