Create a function

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Also, if it is applicable, please see homework policy.

Without knowing the data layout, it's had to suggest an approach. With the right layout it's doable, though.