[Rd] mapply(): Special case of USE.NAMES=TRUE with recent R-devel updates

Hervé Pagès hp@ge@@on@g|thub @end|ng |rom gm@||@com
Wed Dec 1 07:45:15 CET 2021


And also:

   > mapply(paste, c(a="A"), character(), USE.NAMES = TRUE)

   Error in names(answer) <- names1 :

     'names' attribute [1] must be the same length as the vector [0]


When the shortest arguments get recycled to the length of the longest, 
shouldn't their names also get recycled?

   > mapply(paste, c(a="A", b="B"), letters[1:6], USE.NAMES=TRUE)

       a     b  <NA>  <NA>  <NA>  <NA>

   "A a" "B b" "A c" "B d" "A e" "B f"

That's assuming that rep() accurately materializes recycling (I hope it 
does):

   > rep(c(a="A", b="B"), length.out=6)

     a   b   a   b   a   b

   "A" "B" "A" "B" "A" "B"


   > rep(c(a="A", b="B"), length.out=0)

   named character(0)


I always wished that the process of recycling which happens everywhere 
all the time in R was implemented in its own dedicated function 
recycle(). But that's another story.

Anyways, back to mapply(): Once what happens to the names during 
recycling is clarified, there should be no need to be explicit about 
what should happen when the length "of the first ... argument" is zero 
because it will no longer be a special case.

Cheers,
H.


On 30/11/2021 22:10, Henrik Bengtsson wrote:
> Hi,
> 
> in R-devel (4.2.0), we now get:
> 
>> mapply(paste, "A", character(), USE.NAMES = TRUE)
> named list()
> 
> Now, in ?mapply we have:
> 
> USE.NAMES: logical; use the names of the first ... argument, or if
> that is an unnamed character vector, use that vector as the names.
> 
> This basically says we should get:
> 
>> answer <- list()
>> first <- "A"
>> names(answer) <- first
> 
> which obviously is an error. The help is not explicit what should
> happen when the length "of the first ... argument" is zero, but the
> above behavior effectively does something like:
> 
>> answer <- list()
>> first <- "A"
>> names(answer) <- first[seq_along(answer)]
>> answer
> named list()
> 
> Is there a need for the docs to be updated, or should the result be an
> unnamed empty list?
> 
> /Henrik
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Hervé Pagès

Bioconductor Core Team
hpages.on.github using gmail.com



More information about the R-devel mailing list