R:在交叉表中显示小计

R: display subtotals in crosstables

对于报告,我需要创建一些具有多个层次结构的交叉表(使用 r-markdown)。

让我们做一些假数据:

n <- 100
df <- data.frame(
                 language = sample(c("Ger", "Eng", "Spa"), size = n, replace = TRUE),
                 sex = sample(c("female", "male"), size = n, replace = TRUE),
                 agegrp = sample(c("<=30", ">30"), size = n, replace = TRUE),
                 smoker = sample(c("yes", "no"), size = n, replace = TRUE)
                 )

现在我可以使用 table()addmargins() 创建简单的交叉表作为总计:

        Agegroup
Language <=30 >30 Sum
     Eng   16  16  32
     Ger   14  13  27
     Spa   17  24  41
     Sum   47  53 100

很好,但现在我需要创建一些具有层次结构的高级交叉表。这就是我使用 ftable() 函数的原因。例如:

> ftable(Language = df$language, Sex = df$sex, Smoker = df$smoker)
                Smoker no yes
Language Sex                 
Eng      female         7   9
         male           7   9
Ger      female         5  11
         male           4   7
Spa      female        12  10
         male          11   8

有没有办法或者有什么特殊的包来计算总计或小计?

例如我想按行计算总计 and/or 按列计算:

                Smoker no yes Sum
Language Sex                 
Eng      female         7   9  16
         male           7   9  16
Ger      female         5  11  16
         male           4   7  11
Spa      female        12  10  22
         male          11   8  19
Sum                    46  54 100   

或者是否有任何函数或“语法”来定义要计算哪些小计?例如:

                Smoker no yes
Language Sex                 
Eng      female         7   9
         male           7   9
         Subtotal      14  18
Ger      female         5  11
         male           4   7
         Subtotal       9  18
Spa      female        12  10
         male          11   8
         Subtotal      23  18
ftable(addmargins(table(df[c('language', 'sex', 'smoker')])))
                smoker  no yes Sum
language sex                      
Eng      female          9  10  19
         male           10   9  19
         Sum            19  19  38
Ger      female          7   5  12
         male            9   8  17
         Sum            16  13  29
Spa      female         11   8  19
         male            9   5  14
         Sum            20  13  33
Sum      female         27  23  50
         male           28  22  50
         Sum            55  45 100



ftable(addmargins(table(df[c('language', 'sex', 'smoker')]), 2))
                smoker no yes
language sex                 
Eng      female         9  10
         male          10   9
         Sum           19  19
Ger      female         7   5
         male           9   8
         Sum           16  13
Spa      female        11   8
         male           9   5
         Sum           20  13