R programming help to solve the problem

Hi @Lena, and welcome!

This is a hard question to provide any useful guidance without a reproducible example, called a reprex. Your code goes a long way, but without sample data, and a description of how that fails to work (preferably an error message), there's not much that can be offered.

An aside, seeds are used to generate pseudo-randomness. Setting a seed allows reproducing a particular pseudo-random use, and it's entirely normal to see different results with different seeds.