为什么我的意外事件table中有多余的行和列?

Why are there redundant rows and columns in my contingency table?

我是R新手,目前正在学习应急table。我想使用来自“loans_full_schema”(来自 openintro)的数据以及“application_type”和“homeownership”数据来创建应急 table。下面是我的代码。

library(oibiostat)

data("loans_full_schema")

tab <- table(loans_full_schema$application_type, loans_full_schema$homeownership)
tab

我的结果是my outcome

然而,我希望能够得到如下结果wanted outcome
所以我的问题是为什么我的结果中有一个“任何”列和一个空白行?

那是因为数据中有空位

levels(loans_full_schema$homeownership)
#[1] ""         "ANY"      "MORTGAGE" "OWN"      "RENT"    

levels(loans_full_schema$application_type)
#[1] ""           "individual" "joint"     

您可以使用 droplevels 删除它们。

loans_full_schema <- droplevels(loans_full_schema) 

table(loans_full_schema$application_type, loans_full_schema$homeownership)
            
#             MORTGAGE  OWN RENT
#  individual     3839 1170 3496
#  joint           950  183  362

您可以使用 addmargins 来添加总数。

addmargins(table(loans_full_schema$application_type, loans_full_schema$homeownership))

#             MORTGAGE   OWN  RENT   Sum
#  individual     3839  1170  3496  8505
#  joint           950   183   362  1495
#  Sum            4789  1353  3858 10000