I have a data table that I created in R to compare the percentage between a population and sample. Here is the script I created for the table:
team_pop <- team_new %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(pop = n/sum(n)) %>%
arrange(desc(pop)) %>%
adorn_totals()
team_sample <- sample_final %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(sam = n/sum(n)) %>%
arrange(desc(sam)) %>%
adorn_totals()
datatable(
team_pop %>%
select(-n) %>%
left_join(team_sample %>%
select(degree,
sam),
by = "degree"),
rownames = FALSE,
colnames = c(
"Degree"= "degree",
"Population" = "pop",
"Sample" = "sam"),
options = list(info = FALSE,
paging = FALSE,
searching = FALSE)
) %>%
formatPercentage(2, digits = 0) %>%
formatPercentage(3, digits = 0)
Here is what the data looks like:
Degree | Population | Sample |
---|---|---|
Medium | 45% | 43% |
Low | 35% | 37% |
High | 20% | 20% |
But this is how I want me data to be ordered in my data table:
Degree | Population | Sample |
---|---|---|
High | 20% | 20% |
Medium | 45% | 43% |
Low | 35% | 37% |
Just wondering if someone could please help me order my categories based on the Degree column from High to Low. I have tried the R function "sort" using the formula below, but it still orders my data based on the highest number first:
team_pop <- team_new %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(pop = n/sum(n)) %>%
sort(degree, degreasing = FALSE) %>%
adorn_totals()
team_sample <- sample_final %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(sam = n/sum(n)) %>%
sort(degree, degreasing = FALSE) %>%
adorn_totals()
datatable(
team_pop %>%
select(-n) %>%
left_join(team_sample %>%
select(degree,
sam),
by = "degree"),
rownames = FALSE,
colnames = c(
"Degree"= "degree",
"Population" = "pop",
"Sample" = "sam"),
options = list(info = FALSE,
paging = FALSE,
searching = FALSE)
) %>%
formatPercentage(2, digits = 0) %>%
formatPercentage(3, digits = 0)
CodePudding user response:
Make your degree variable a factor and then arrange based on degree.
team_pop <- team_pop %>%
mutate(Degree=factor(Degree, levels = c("High", "Medium", "Low"))) %>%
arrange(Degree)