From cberry at tajo.ucsd.edu  Thu Apr 21 00:49:26 2005
From: cberry at tajo.ucsd.edu (Charles C. Berry)
Date: Thu Apr 21 00:49:44 2005
Subject: [Rd] Negative argument for head() and tail()
In-Reply-To: <200504201632.56138.vincent.goulet@act.ulaval.ca>
References: <200504201426.24523.vincent.goulet@act.ulaval.ca>
	<1abe3fa90504201305190301cd@mail.gmail.com>
	<200504201632.56138.vincent.goulet@act.ulaval.ca>
Message-ID: <Pine.LNX.4.62.0504201514260.5180@tajo.ucsd.edu>


I'd like to second Vincent's appeal.

Before 'head' and 'tail' were added to package:utils, I had 
versions that allowed negative subscripts.

And I found the code rendered by them readable and less subject to 
mental or typographical errors.

I think all that is needed for the versions in package::utils is:

 	if (n < 0 ) n <- length(x) + n

or

 	if (n < 0 ) n <- nrow(x) + n

as the first line of the body of each method except head.function.

Chuck


On Wed, 20 Apr 2005, Vincent Goulet wrote:

> Le 20 Avril 2005 16:05, A.J. Rossini a crit:
>> x[-length(x)] ?
>
> I feel idiot from that one, but hold on to my point. ;-)
>
> Better example, then: how about a compact way to drop, say, the last 3
> elements of a vector? I think
>
>> tail(x, -3)
>
> would be nicer --- and more readable --- than
>
>> x[-((length(x)-2):length(x))]
>
> or
>
>> x[0:3 -length(x)]
>
> or some other convoluted way to achieve the same result.
>
> (BTW, dropping the last element of a vector would be 'tail(x, -1)., not
> 'tail(x, -length(x))' as stated in my original post. The latter would drop
> all the elements.)
>
>> On 4/20/05, Vincent Goulet <vincent.goulet@act.ulaval.ca> wrote:
>>> Dear R developers,
>>>
>>> I'm a former APL programmer. In that language, the "take" (up arrow) and
>>> "drop" (down arrow) operators were extensively used to, well, take and
>>> drop elements of vectors. Functions head() and tail() are equivalents in
>>> R for the "take" operator, but nothing seems to mimic the "drop"
>>> operator. I think it would be useful.
>>>
>>> For example, is there any simpler way to extract all elements of a vector
>>> but the last one than doing
>>>
>>>> x[1:(length(x) - 1)]  ?
>>>
>>> An equivalent of "drop" would make this easy.
>>>
>>> Now, I think this could be easily implemented with the existing functions
>>> by allowing negative arguments to head() and tail(). For example,
>>>
>>>> head(x, -1)
>>>
>>> would drop the first element of a vector and
>>>
>>>> tail(x, -length(x))
>>>
>>> the last one.
>>>
>>> Is there any interest for such an extension?
>>>
>>> I would be very willing to contribute a patch, although I don't have much
>>> experience in doing so.
>>>
>>> Best regards,
>>>
>>> --
>>>   Vincent Goulet, Associate Professor
>>>   cole d'actuariat
>>>   Universit Laval, Qubec
>>>   Vincent.Goulet@act.ulaval.ca   http://vgoulet.act.ulaval.ca
>>>
>>> ______________________________________________
>>> R-devel@stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> -- 
>  Vincent Goulet, Professeur agrg
>  cole d'actuariat
>  Universit Laval, Qubec
>  Vincent.Goulet@act.ulaval.ca   http://vgoulet.act.ulaval.ca
>
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry@tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717


[Rd] Negative argument for head() and tail()
Charles C. Berry