R code for loop need help

See the FAQ: How to do a minimal reproducible example reprex for beginners. Some data is needed to readily answer the question. It doesn't have to be all your data or even your data at all, so long as it behaves the same way. It can even be fake data or from a standard data set.