R expss表格中出现重复行且小计重叠

R expss表格中出现重复行且小计重叠,r,duplicates,expss,R,Duplicates,Expss,继今天早些时候遇到的情况之后,又出现了一个与expss相关的新问题。 在交叉表创建序列期间使用重叠的小计会导致重复的行,更准确地说,是在至少两个不同的小计中使用的行 让我们考虑推断>代码>数据集来强调这一点。为了演示,我们将强制奇偶校验为factor library(datasets) infert$parity <- factor(infert$parity) infert %>% tab_cells(parity) %>% tab_subtotal_cells("

继今天早些时候遇到的情况之后,又出现了一个与expss相关的新问题。 在交叉表创建序列期间使用重叠的小计会导致重复的行,更准确地说,是在至少两个不同的小计中使用的行

让我们考虑<代码>推断>代码>数据集来强调这一点。为了演示,我们将强制

奇偶校验
为factor

library(datasets)
infert$parity <- factor(infert$parity)

infert %>%
  tab_cells(parity) %>%
  tab_subtotal_cells("1+2+3"=levels(parity)[1:3], "1+2"=levels(parity)[1:2],
                     position = "above") %>%
  tab_cols(total()) %>%
  tab_stat_cases(label="N", total_row_position="none") %>%
  tab_pivot(stat_position="inside_columns")

谢谢大家!

如果小计中有重叠项,可以使用名为“hide”的函数隐藏这些项。到目前为止,还没有自定义小计定位的功能。但是,我们可以通过一个小技巧获得您想要的输出:

data("infert")
infert$parity = factor(infert$parity)

infert %>%
    tab_cells(
            subtotal(parity, 
                     "1+2+3"=hide(levels(parity)[1:3]), 
                     "1+2"= levels(parity)[1:2],
                     "3" = hide(levels(parity)[3]), # to show 3, because "1+2+3" subtotal hide its items
                     "5+6"=levels(parity)[5:6], 
                     position = "above"
                     )
    ) %>%
    tab_cols(total()) %>%
    tab_stat_cases(label="N", total_row_position="none") %>%
    tab_pivot(stat_position="inside_columns")

不错的方法,我尝试了
tab\u subtotal\u cells
tab\u net\u cells
的组合,但没有想到将其包含在
tab\u cells
函数中。无论如何,这一切都很好,谢谢你@Gregory Demin!
data("infert")
infert$parity = factor(infert$parity)

infert %>%
    tab_cells(
            subtotal(parity, 
                     "1+2+3"=hide(levels(parity)[1:3]), 
                     "1+2"= levels(parity)[1:2],
                     "3" = hide(levels(parity)[3]), # to show 3, because "1+2+3" subtotal hide its items
                     "5+6"=levels(parity)[5:6], 
                     position = "above"
                     )
    ) %>%
    tab_cols(total()) %>%
    tab_stat_cases(label="N", total_row_position="none") %>%
    tab_pivot(stat_position="inside_columns")