group by and summarize with conditional sum

You may see more answers if you edit to include a a reprex (see the FAQ. Although there is code here, it's not in usable form. To fix

dput(df)

and cut-and-paste the output enclosed in triple backtick code blocks.