Ok, so I have a table that looks a little something like this for the fist several rows:
| Department | Diagnosis Code |
|---|---|
| Dept. 1 | Code1 |
| Dept. 2 | Code2 |
| Dept. 3 | Code3 |
| Dept. 3 | Code3 |
| Dept. 3 | Code4 |
| Dept. 4 | Code4 |
| Dept. 4 | Code4 |
| Dept. 4 | Code5 |
| Dept. 4 | Code5 |
| Dept. 4 | Code5 |
What I want is to develop a table that looks like this:
| Department | Code1% | Code2% | Code3% | Code4% | Code5% |
|---|---|---|---|---|---|
| Dept. 1 | xx% | xx% | xx% | xx% | xx% |
Where the above percentages are the percentage of each code for each department, ie it's the total number of times Code1 appears within Department 1, divided by the total number of occurrences of "Code instances" within Department 1. So if Code 1 appeared 50 times in Department 1, and Department 1 had 120 recorded Department-Code instances across all Codes, the percent ought to be 50/120.
I'm trying to use some combination of group_by(), mutate(), and summarise() to get the job done, but I'm having trouble figuring out how to properly combine and write code to get the output I want.
I've seen a lot example code showing something similar when the second column is of some numeric frequency type, but I have yet to find something that does the same when the second column consists of strings corresponding to discrete categories.
**EDIT: Also, the codes are an alphanumeric. For example, one code may be something like E77.09, while another might be something like C30, and another might be D24.3