Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a table in R by count

I have created a function in R to create a table that gives a count and a percentage:

tblFun <- function(x){
tbl <- table((x))
res <- cbind(tbl,round(prop.table(tbl)*100,0))
colnames(res) <- c('Count','Percentage')
res}

then to execute it I run it against a field in my dataset and output using kable:

region <-tblFun(mtcars$mpg)
knitr::kable(region)

this gives a table sorted by the factor name, however I want to sort by the count or percentage. enter image description here

I've tried the sort functions I know of. I couldn't use the tidyverse library functions either as they wouldn't give me the correct percentage:

library(dplyr)
region <- na.omit(mtcars) %>% 
  group_by(mtcars$mpg) %>%
  summarize(Count=n()) %>%
  mutate(Percent = round((n()/sum(n())*100))) %>%
  arrange(desc(Count))
knitr::kable(region)

enter image description here

a fix to any of them would be greatly appreciated.

like image 475
Chris Avatar asked Aug 31 '25 05:08

Chris


2 Answers

I just fixed your code, as below. You just needed count instead of n():

library(dplyr)
na.omit(mtcars) %>% 
  group_by(mtcars$mpg) %>%
  summarize(Count=n()) %>%
  mutate(Percent = round((Count/sum(Count)*100))) %>%
  arrange(desc(Count))


 # A tibble: 25 x 3
 #     `mtcars$mpg` Count Percent
 #           <dbl> <int>   <dbl>
 # 1         10.4     2       6
 # 2         15.2     2       6
 # 3         19.2     2       6
 # 4         21.0     2       6
 # 5         21.4     2       6
 # 6         22.8     2       6
 # 7         30.4     2       6
 # 8         13.3     1       3
 # 9         14.3     1       3
 #10         14.7     1       3
 # ... with 15 more rows
like image 82
Santosh M. Avatar answered Sep 02 '25 19:09

Santosh M.


I think you want to calculate Percent differently

library(tidyr)
library(dplyr)
library(knitr)

mtcars %>% 
  drop_na %>%
  group_by(mpg) %>%
  summarize(
    count = n(),
    percent = count / nrow(.) * 100
  ) %>%
  arrange(desc(count), desc(mpg)) %>%
  head(10) %>%
  kable

#   |  mpg| count| percent|
#   |----:|-----:|-------:|
#   | 30.4|     2|   6.250|
#   | 22.8|     2|   6.250|
#   | 21.4|     2|   6.250|
#   | 21.0|     2|   6.250|
#   | 19.2|     2|   6.250|
#   | 15.2|     2|   6.250|
#   | 10.4|     2|   6.250|
#   | 33.9|     1|   3.125|
#   | 32.4|     1|   3.125|
#   | 27.3|     1|   3.125|
like image 24
Kevin Arseneau Avatar answered Sep 02 '25 19:09

Kevin Arseneau