From Thom@@@SOEIRO @end|ng |rom @p-hm@|r  Sun Oct 10 13:56:00 2021
From: Thom@@@SOEIRO @end|ng |rom @p-hm@|r (SOEIRO Thomas)
Date: Sun, 10 Oct 2021 11:56:00 +0000
Subject: [Rd] Potential bugs in table dnn
Message-ID: <1633866959731.85310@ap-hm.fr>

Dear list,

table does not set dnn for dataframes of length 1:

table(warpbreaks[2:3]) # has dnn
#     tension
# wool L M H
#    A 9 9 9
#    B 9 9 9

table(warpbreaks[2]) # has no dnn
# 
#  A  B 
# 27 27 

This is because of if (length(dnn) != length(args)) (line 53 in https://github.com/wch/r-source/blob/trunk/src/library/base/R/table.R). When commenting this line or modifying it to if (length(dnn) != length(args) || dnn == ""), dnn are set as expected:

table2(warpbreaks[2:3]) # has dnn
#     tension
# wool L M H
#    A 9 9 9
#    B 9 9 9

table2(warpbreaks[2]) # has dnn
# wool
#  A  B 
# 27 27 

However, I do not get the logic for the initial if (length(dnn) != length(args)), so the change may break something else...

In addition, table documentation says "If the argument dnn is not supplied, the internal function list.names is called to compute the ?dimname names?. If the arguments in ... are named, those names are used." Some cases seem inconsistent or may return a warning:

table(warpbreaks[2], dnn = letters) # no warning/not as documented
# wool
#  A  B 
# 27 27 

table(warpbreaks[2], dnn = letters[1]) # as documented
# a
#  A  B 
# 27 27 

table(zzz = warpbreaks[2], dnn = letters[1]) # as documented
# a
#  A  B 
# 27 27 

table(zzz = warpbreaks$wool, dnn = letters[1]) # as documented
# a
#  A  B 
# 27 27 

table(warpbreaks$wool, dnn = letters) # as expected
# Error in names(dn) <- dnn : 
#   attribut 'names' [26] doit ?tre de m?me longueur que le vecteur [1]

Best regards,

Thomas


[Rd] Potential bugs in table dnn
SOEIRO Thomas