R multiply multiple columns by constant if that row is for a group

So I have some sports data and I need to multiply all of the 2020 data by 1.25 because the games were shorter than usual during the season but leave the other years' data as it is.

So if I have

Season Player Kicks Tackles
2019 A 3 5
2019 B 1 2
2019 C 10 10
2020 A 2 4
2020 B 1 1
2020 C 8 8

What I want to do is multiply kicks and tackles by 1.25 if the Season is 2020

Season Player Kicks Tackles
2019 A 3 5
2019 B 1 2
2019 C 10 10
2020 A 2.5 5
2020 B 1.25 1.25
2020 C 10 10

What is the best way to do this without having to send the 2020 data to it's own data frame and the appending it to the previous data after I multiply it?

I'm assuming there's a way to do it with mutate_if



Read more here: https://stackoverflow.com/questions/66326193/r-multiply-multiple-columns-by-constant-if-that-row-is-for-a-group

Content Attribution

This content was originally published by Dave Matteo at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: