Help with some R code

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. It can be really difficult reverse engineering a question from just a description, and screenshots are usually not helpful.