I'm trying to calculate the cumulative time among several grades.

Here's how my original `df` looks like:

```
df = data.frame(id = c(1,1,1,1,2,2,2,2),
                group = c(0,0,0,0,1,1,1,1),
                grade = c(0,1,2,3,0,1,3,4),
                time = c(10,7,4,1,20,17,14,11))

```

Here's what I'm expecting as the result `df1`:

```
df1 = df %>%
  pivot_wider(
    names_from = "grade",
    names_prefix = "grade_",
    values_from = "time") %>%
  replace(is.na(.), 0) %>%

  mutate(grade_1 = grade_1 + grade_2 + grade_3 + grade_4,
         grade_2 = grade_2 + grade_3 + grade_4,
         grade_3 = grade_3 + grade_4) %>%

  pivot_longer(
    cols = 3:7,
    names_to = "grade",
    names_prefix = "grade_",
    values_to = "time") 

```

My method works, but I want it to be more flexible. When I have more grades in the df, I don't need to manually add grade\_x = grade\_1 + grade\_2 + grade\_3 ...

Thank you!