Function with Rules

It is better but it is still hard to help you, since your sample data is not on a copy/paste friendly format and your example is not self-contained, please follow this guide and try to turn this into a proper reproducible example.

Note: Even if you are just practicing R for your own, it is better not to include verbatim instructions, since content might be protected and reproduction restricted.