I would like to take an .xlsx file of data in columns, and turn it into a pivot table in which a subset of column data is summed based on row groups.
I have a table that looks like this:
| Class | Instructor | ContactHours|
| --------| ------------| ------------|
| Class A | Instructor A| 7.2 |
| Class B | Instructor A| 5 |
| Class C | Instructor A| 3 |
| Class D | Instructor B| 3 |
| Class E | Instructor B| 3 |
| Class F | Instructor C| 7.4 |
| Class G | Instructor D| 7.4 |
| Class H | Instructor D| 5 |
And want to total the Contact Hours (CH) based on rows of Instructors, like this:
| Instructor | Class | CH |
| -------------| -------| --- |
| Instructor A | | 15.2|
| | Class A| 7.2 |
| | Class B| 5 |
| | Class C| 3 |
| Instructor B | | 6 |
| | Class D| 3 |
| | Class E| 3 |
| Instructor C | | 7.4 |
| | Class F| 7.4 |
| Instructor D | | 12.4|
| | Class G| 7.4 |
| | Class H| 5 |
I have tried pivottablr and other packages and have gotten the furthest with pivottablr.
With this code I am able to get all but the column of Total CH. Instead I get the column but it is full of zeroes.
pat <- read_csv("test.csv", skip=2, col_names = TRUE)
pt <- PivotTable$new()
pt$addData(pat)
pt$addRowDataGroups("Instructor", addTotal=TRUE)
pt$addRowDataGroups("Class", addTotal=FALSE)
pt$addRowDataGroups("ContactHours", addTotal=FALSE)
pt$defineCalculation(calculationName="TotalCH", summariseExpression="sum()")
pt$renderPivot()
| Instructor | Class | CH | TotalCH |
| -------------| -------| --- |---------|
| Instructor A | Class A| 7.2 | 0 |
| | Class B| 5 | 0 |
| | Class C| 3 | 0 |
| Instructor B | Class D| 3 | 0 |
| | Class E| 3 | 0 |
| Instructor C | Class F| 7.4 | 0 |
| Instructor D | Class G| 7.4 | 0 |
| | Class H| 5 | 0 |
But I have also gotten (mostly) what I want with dplyr
pat %>%
group_by(Class) %>%
group_by(Instructor) %>%
mutate(sum_ContactHours = sum(ContactHours)) %>%
ungroup()
Which gives me this:
# A tibble: 8 × 4
Class Instructor ContactHours sum_ContactHours
<chr> <chr> <dbl> <dbl>
1 Class A Instructor A 7.2 15.2
2 Class B Instructor A 5 15.2
3 Class C Instructor A 3 15.2
4 Class D Instructor B 3 6
5 Class E Instructor B 3 6
6 Class F Instructor C 7.4 7.4
7 Class G Instructor D 7.4 12.4
8 Class H Instructor D 5 12.4