We really need a reproducible example (reprex), preferably a Minimum Working Example. At the moment we have no idea of what your data looks like or what those loops are expected to do nor do we know if you are getting any error or warning messages.
Have a look at :
and