I want to fill NA rows in a data table by the mean values for unique value from another column. Please see the intended output. How can I achieve this in R? I prefer the data table output.
data2 <- data.table(Plan=c(11,11,11,11,91,91,91,91), Price=c(4.4,4.4,4.4,NA,3.22,3.22,3.22,NA), factor=c(0.17,0.17,0.17,NA,0.15,0.15,0.15,NA), Type=c(4,4,4,4,3,3,3,3))
data2
Plan Price factor Type
1: 11 4.40 0.17 4
2: 11 4.40 0.17 4
3: 11 4.40 0.17 4
4: 11 NA NA 4
5: 91 3.22 0.15 3
6: 91 3.22 0.15 3
7: 91 3.22 0.15 3
8: 91 NA NA 3
Output
Plan Price factor Type
1: 11 4.40 0.17 4
2: 11 4.40 0.17 4
3: 11 4.40 0.17 4
4: 11 4.40 0.17 4
5: 91 3.22 0.15 3
6: 91 3.22 0.15 3
7: 91 3.22 0.15 3
8: 91 3.22 0.15 3