harrelfe / Hmisc

Harrell Miscellaneous
Other
208 stars 81 forks source link

Hmisc::wtd.table inconsistent with base::table for 0-count factor levels #184

Closed jeffkeller-einc closed 1 month ago

jeffkeller-einc commented 4 months ago

When tabulating factors (with or without weights), I noticed that wtd.table(..., type = "table") is not consistent with table from base R in the presence of factor levels without observations.

x <- factor(c("A", "B"), levels = c("A", "B", "C"))
table(x)
# A B C 
# 1 1 0 
Hmisc::wtd.table(x, type = "table")
# A B 
# 1 1 

Unless there is a specific reason for this, I think it would be best to remain consistent with table.

harrelfe commented 1 month ago

You’re probably right but I didn’t intend for complete compatibility. Patches to Hmisc::wtd.table are welcome here.