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

From: Martin Maechler <>
Date: Tue 18 Jul 2006 - 08:42:22 GMT

>>>>> "Vincent" == Vincent Goulet <>
>>>>> 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.

    Vincent> I put the patched head.R and head.Rd files, along with diff files in     Vincent>

    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.

    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}

    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' ?

    Vincent> Best regards,

Bonnes salutations,

    Vincent> -----
    Vincent> [1] See previous messages to r-devel by myself 
    Vincent> <>

    Vincent> and Bill Venables
    Vincent> <> mailing list Received on Tue Jul 18 18:45:50 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 Wed 26 Jul 2006 - 16:27:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.