'How can I write a nested for loop or other function in R to solve problem

I have a dataset with more than 1 duplicate for each product. Each duplicate datapoint needs to be added with the rest, to end up with the mean of all the duplicates for each product. Example(See image) : (CAL_A1_ABCR_0 + CAL_A2_ABCR_0 + CAL_A3_ABCR_0...)/n = result. And the same goes for the other products. Since this is an iterative and time consuming task for the whole dataset, I was trying to make a nested for loop but can't seem to do it right. Doe anyone have any solution how to tackle the problem with the nested for loop or any other function ? screenshot image of dataset



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source