排序expss输出表

时间:2018-07-10 14:58:55

标签: r sorting spss expss

我想用排序的频率数据生成一个expss表,请在线获取以下示例

library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)
mtcars %>% calc_cro_cpct(cyl, list(total(), am, vs))

这将导致以下输出: enter image description here

但是,我希望能够通过“ #Total cases”行的降序对Table1进行排序。我能够使用tab_sort_desc命令来创建列,但是每当我在该命令中选择一行时,我都会收到“错误:找不到名称:...”

可以添加到上面的代码中以按值对行进行排序的任何命令吗?

1 个答案:

答案 0 :(得分:0)

最简单的方法似乎是表转置:

mtcars %>% 
    calc_cro_cpct(cyl, list(total(), am, vs)) %>% 
    tab_transpose() %>% # transpose
    tab_sort_desc(., ncol(.)) %>% # sort by total
    tab_transpose() # reverse transposition