I have a dataset with three periods for each ID. I this way, I am trying to fill NA values in my dataset with the mean if there are 2/3 values without NA (the mean of the existing values by id). On the other hand, If there is just 1/3 value without NA, I want to fill the rest with that value. My dataset looks like this:
ID value period
1203 -0.64 1
1203 2
1203 -0.12 3
1218 0.495 1
1218 2
1218 3
So, I want to fill the NA value in the ID 1203 with the mean of -0.64 and -0.12, and for the ID 1218 I want to fil with 0.495.