Mark Evans
Mark Evans

Reputation: 81

Table1() Descriptive Statistics in R

Are there any table1 users that know how to sort a table descending by frequency? Example in the table attached do you know how to sort to 14,11,7?

library(table1)
mtcars$cyl <- as.factor(mtcars$cyl)
table1(~mtcars$cyl, data = mtcars)

enter image description here

Upvotes: 3

Views: 953

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

One way would be to arrange the factor levels based on frequency of cyl.

mtcars$cyl <- factor(mtcars$cyl, levels = as.integer(names(sort(table(mtcars$cyl),
                                  decreasing = TRUE))))
table1::table1(~cyl, data = mtcars)

enter image description here

Upvotes: 4

Related Questions