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.
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)
a fix to any of them would be greatly appreciated.
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
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|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With