[Rd] Potential bugs in table dnn

SOEIRO Thomas Thom@@@SOEIRO @end|ng |rom @p-hm@|r
Sun Oct 10 13:56:00 CEST 2021


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


More information about the R-devel mailing list