[Rd] Patch to allow negative argument in head() and tail()
Vincent Goulet
vincent.goulet at act.ulaval.ca
Tue Jul 18 19:56:06 CEST 2006
Le Mardi 18 Juillet 2006 04:42, Martin Maechler a écrit :
> >>>>> "Vincent" == Vincent Goulet <vincent.goulet at 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
More information about the R-devel
mailing list