[Rd] inconsistency in treatment of USE.NAMES argument

Martin Maechler maechler at stat.math.ethz.ch
Tue Feb 9 12:37:56 CET 2016


>>>>> Hervé Pagès <hpages at fredhutch.org>
>>>>>     on Mon, 8 Feb 2016 10:48:50 -0800 writes:

    > Hi,
    > Both vapply() and sapply() support the 'USE.NAMES' argument. According
    > to the man page:

    > USE.NAMES: logical; if ‘TRUE’ and if ‘X’ is character, use ‘X’ as
    >    ‘names’ for the result unless it had names already.

    > But if 'X' has names already and 'USE.NAMES' is FALSE, it's not clear
    > what will happen to the names. Are they going to propagate to the
    > result or not? Unfortunately, vapply() and sapply() give a different
    > answer:

    >> vapply(list(A="a", B=1:2), is.integer, logical(1), USE.NAMES=FALSE)
    > [1] FALSE  TRUE

    >> sapply(list(A="a", B=1:2), is.integer, USE.NAMES=FALSE)
    > A     B
    > FALSE  TRUE

This is very unfortunate, and I was not aware of this.

You know that sapply()  is an order of magnitude older than vapply()
and you probably don't know that lapply() is also somewhat older
than sapply() [but that part is pre-R (but S-) history ...]
which explains part:

1) lapply() does *not* have a  USE.NAMES  argument and it
   always keeps names when they are there in X.

2) sapply() has been designed as "s"implified l"apply" where
   in this case "simplified" also was to mean "user-friendly" /
   "simple to use".
   For that reason,
   a) sapply() also keeps names when they are there (as lapply).
   b) If USE.NAMES=TRUE (as by default) is also constructs names
      in cases where lapply() does not contain, i.e., in case of
      character X.

3) IIRC, the goals for vapply() had been  "like sapply", with two advantages:
   a. faster
   b. "error-checking" in the sense of ensuring consistent
       results of the single function calls.

    > Wouldn't it make sense to have vapply() and sapply() treat the
    > 'USE.NAMES' argument consistently? 

Yes, but from what I wrote above, I believe  vapply() would have
to change.

Martin


    > The behavior of vapply() seems
    > to make more sense to me. Note that it's consistent with what mapply()
    > does:

    >> mapply(is.integer, list(A="a", B=1:2), USE.NAMES=FALSE)
    > [1] FALSE  TRUE

    > If the behavior of sapply() cannot be changed, at least the man page
    > could clarify what happens when 'USE.NAMES' is FALSE, which is
    > different for each function.

    > Thanks,
    > H.

    > -- 
    > Hervé Pagès

    > Program in Computational Biology
    > Division of Public Health Sciences
    > Fred Hutchinson Cancer Research Center
    > 1100 Fairview Ave. N, M1-B514
    > P.O. Box 19024
    > Seattle, WA 98109-1024

    > E-mail: hpages at fredhutch.org
    > Phone:  (206) 667-5791
    > Fax:    (206) 667-1319

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list