0

I have R code that takes raw data where each patient entry is one row, and sums it up for a 'frequency' column for each department by date.

What I used here was the code:

department_totals <- as.data.frame(count(sheet, c("Date", "Department")))

To get:

Department Date Frequency
Dental 14 Mar 5
Dental 15 Mar 3
Dental 16 Mar 2
Cardio 14 Mar 4
Cardio 15 Mar 7
Cardio 16 Mar 8
Physio 14 Mar 1
Physio 16 Mar 2

But for this new project, I need it to be the actual individual departments by date, like this:

Date Dental Cardio Physio
14 Mar 5 4 1
15 Mar 3 7 blank
16 Mar 2 8 2

And I can't figure out how to do it. I can group by department, but I'm trying to make each unique variable in 'Department' its own variable and then have the frequency of variables for each of those as a new column, ordered by date.

The intent here is to be able to make line graphs of how each of these departments' frequency of patients changes over time.

Ali
  • 1
  • 1

1 Answers1

0
library(tidyverse)
df %>% pivot_wider(names_from = Department, values_from = Frequency)


# A tibble: 3 x 4
  Date   Dental Cardio Physio
  <chr>   <int>  <int>  <int>
1 14_Mar      5      4      1
2 15_Mar      3      7     NA
3 16_Mar      2      8      2
Adam Quek
  • 6,973
  • 1
  • 17
  • 23