If anyone could help with this would be appreciated. I am trying to add two new variables to
my existing DF (*Grade1Sum and *Grade2Sum) summing each film's grade (ET got graded one three times so the *Grade1Sum total is always equal to 3 for example)
There are only two grades (1 and 2). Hopefully this makes sense, so if anyone can help, would be appreciated (I want to add them to an existing DF, thus currently I only have the first three columns).
I am adding what I want the DF to look like as a pic as it won't format correctly:
**Additional does anyone know how to count the amount of times each grade was received per film?
CodePudding user response:
Here is one way to do it
df %>%
group_by(Film) %>%
mutate(Grade1Sum=sum(Grade[Grade==1]), Grade2Sum=sum(Grade[Grade==2]))
Here is a more flexible way to do it
df %>%
group_by(Film, Grade) %>%
summarise(Sum=sum(Grade)) %>%
pivot_wider(names_from=Grade, values_from=Sum,
names_glue="Grade{Grade}{.value}") %>%
right_join(., df)