@FJCC thank you very much for your reply. Perhaps I was not very clear in my initial post to accurately explain the sorting.
I wanted the dataframe to be sorted in two ways
A. The data should be sorted on region (ascending order) and the total period-region count values should be on descending order to match the following
| period |
region |
Count of case |
| 1 |
Eastern |
33 |
|
Central |
15 |
|
Western |
6 |
| 1 Total |
|
54 |
| 2 |
Eastern |
50 |
|
Central |
15 |
|
Western |
3 |
| 2 Total |
|
68 |
| 3 |
Eastern |
66 |
|
Central |
20 |
|
Western |
5 |
| 3 Total |
|
91 |
| 4 |
Eastern |
27 |
|
Central |
15 |
|
Western |
4 |
| 4 Total |
|
46 |
| 5 |
Eastern |
30 |
|
Central |
18 |
|
Western |
6 |
| 5 Total |
|
54 |
| 6 |
Eastern |
40 |
|
Central |
13 |
|
Western |
3 |
| 6 Total |
|
56 |
| Grand Total |
|
369 |
B. Now once this is done, I want the subset inside each period-region to be sorted in descending order.
I have solved this in the following way
library(openxlsx)
library(dplyr)
library(tidyverse)
df <- read.xlsx("C:/Users/smpao1/Documents/Exercises/PBIR/SampleRADN.xlsx")
df1<-data.frame(df)
df2<-df1%>%count(period,region)
df3<-df2[order(df2$period, -df2$n),]
df4<-df1%>%count(period,region,manager,employee)
df5<-df4[order(df4$period, -df4$n),]
df6<-left_join(df3,df5,by=c("period"="period","region"="region"))
df7<-df6%>% select(period, region, manager, employee,n.y)
Now I can plot the data as per the order I had in my mind. However, if there is a better way of doing it and you can show me, I would be very eager to learn that.
Thanking you in advance.