I have written a R snippet with the corresponding loop, which I have realized relatively fast. Thank you very much.