I think it’s going to be hard for helpers to understand your problem without some more information. Can you please try creating a small, self-contained reproducible example that illustrates what you’re trying to do? It’s ok if it doesn’t quite work! But your example should at least include the relevant packages you’re loading and only refer to data that’s available to others. Check out the link for more details on how to do this!
It also might help to explain a little bit more about what you’re doing (I’m guessing writing some sort of simulation??). Experienced R users come from all different backgrounds, so people won’t necessarily automatically understand the goals of your code without some context.