conditional manipulating of reoccuring variables

See the FAQ: How to do a minimal reproducible example reprex for beginners. Without truly representative data, an answer should not be expected to cover any cases other those given.