Re: [Rd] Patch to allow negative argument in head() and tail()

From: Vincent Goulet <vincent.goulet_at_act.ulaval.ca>
Date: Tue 18 Jul 2006 - 17:56:06 GMT

Le Mardi 18 Juillet 2006 04:42, Martin Maechler a écrit :
> >>>>> "Vincent" == Vincent Goulet <vincent.goulet@act.ulaval.ca>
> >>>>> on Mon, 17 Jul 2006 15:03:34 -0400 writes:
>
> Vincent> Dear developeRs (and other abuseRs ;-),
>
> Vincent> I would like to contribute a patch against
> Vincent> functions head() and tail() of package utils to
> Vincent> allow for a negative 'n' argument. This allows to
> Vincent> extract all but the first/last 'n'
> Vincent> elements/rows/lines of an object, similar to the
> Vincent> "drop" operator of APL. [1]
>
> Hmm, if you reread Bill Venables proposal (URL below), you did
> something different : In Bill's (and my!) "book",
>
> head would always give the *first* few entries and
> tail would always give the *last* few entries.
>
> That's different from APL's drop, but for a good reason:
> The words 'head' and 'tail' exactly suggest so.

Indeed, I did not pay that much attention to the second paragraph.

For me, this usage of head() and tail() is, at first, completely unintuitive since I more used to, say, "start from the beginning (head) of the vector and drop the first n elements" than "return the end of the vector except the first n elements". But I must agree your convention does make sense!

> Vincent> I put the patched head.R and head.Rd files, along with diff
> files in Vincent> http://vgoulet.act.ulaval.ca/pub/R/
>
> Vincent> The differences were obtained against today's version of
> r-devel (more Vincent> specifically revision 30277 of head.R and revision
> 30915 of head.Rd).
>
> That's good (to take the "current" sources for the diffs).
>
> Vincent> Some comments:
>
> Vincent> - The current version of head() and tail() will
> Vincent> accept a vector of length > 1 for argument 'n' but
> Vincent> will silently use the smallest value. This became
> Vincent> awkward to reproduce in my versions and did not
> Vincent> seem interesting anyway. Instead, I added an error
> Vincent> message if length(n) > 1.
>
> that's ok in my view
>
> Vincent> - I used the word "scalar" in the aforementioned
> Vincent> error message to mean a vector of length 1. Perhaps
> Vincent> is this not the correct R terminology?
>
> indeed, it's rarely used in R terminology; for one reason
> because S (and hence R) does not differentiate between length-1
> vectors and scalars the way APL does.

So I should rephrase the message as "n should be a vector of length 1", then.

> Vincent> - I added a 'addrownums = TRUE' argument to head() used when n
> < 0, similar to Vincent> tail() with n > 0. This required to write separate
> methods for Vincent> classes 'data.frame' and 'matrix'.
>
> seems not unreasonable {I did not yet look at your implementation there}

Actually, this will no longer be needed since head() will always return, well, the head of an object, hence no need to relabel the lines of a matrix.

> Vincent> - The 'function' methods are not modified.
>
> Vincent> - In the man page, the 'function' method was not documented in
> the usage Vincent> section. Done now.
>
> ok, though not necessary: The recommended approach is to only
> document methods when they have ``surprising arguments'', i.e.,
> arguments not in the generic function.
>
> In our case, 'n = 6' is not part of the generic, so strictly
> speaking *is* a "surprising argument".
> Probably it was not made part of the generic, since it's
> imaginable to have objects whose "head" is always of a fixed
> given size, and where specifying 'n' does not make sense.
>
> Vincent> - I don't think the patch would break any existing code,
> except code using the Vincent> (undocumented) "feature" mentioned in my
> first remark, above.

>
> Vincent> I hope you will find my (albeit small) contribution useful.
>
> Yes, but can you change it such that head() does give the "head"
> and tail() the "tail" also for negative 'n' ?

I'll give it a go in the next couple of days.

Bye! Vincent

> Vincent> Best regards,
>
> Bonnes salutations,
> Martin
>
> Vincent> -----
> Vincent> [1] See previous messages to r-devel by myself
> Vincent>
> <https://stat.ethz.ch/pipermail/r-devel/2005-April/032881.html>
>
> Vincent> and Bill Venables
> Vincent> <https://stat.ethz.ch/pipermail/r-devel/2005-May/033081.html>

-- 
  Vincent Goulet, Associate Professor
  École d'actuariat
  Université Laval, Québec 
  Vincent.Goulet_at_act.ulaval.ca   http://vgoulet.act.ulaval.ca

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Jul 19 03:58:26 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 18 Jul 2006 - 22:28:01 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.