2

I would like to generate an expss table with sorted frequency data, take the below example available online

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))

This leads to this output: enter image description here

However, I would like to be able to sort Table1 by descending value of the "#Total cases" row. I was able to use the tab_sort_desc command for columns, but whenever I select a row in this command I get 'Error: names not found: ..."

Any command that can be added to the code above to sort row by value?

zx8754
  • 52,746
  • 12
  • 114
  • 209
gfa2001
  • 227
  • 1
  • 2
  • 10

1 Answers1

0

It seems that simplest method is table transposition:

mtcars %>% 
    calc_cro_cpct(cyl, list(total(), am, vs)) %>% 
    tab_transpose() %>% # transpose
    tab_sort_desc(., ncol(.)) %>% # sort by total
    tab_transpose() # reverse transposition
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • Thanks, this is good to know. Since I'm a beginner at coding, excuse my naive question, but does '.' default to total? can it be replaced by a specific row for which the frequencies would be sorted? – gfa2001 Jul 10 '18 at 22:31
  • @gfa2001 '.' means previous result (left-hand side of `%>%`). So `ncol(.)` means the last column of the table which is '#Total cases'. You can replace `ncol(.)` with desired column number. – Gregory Demin Jul 11 '18 at 11:02