我想用另一列中唯一值的平均值填充数据表中的NA行。请查看预期的输出。如何在R中实现这一目标?我更喜欢数据表输出。
data2 <- data.table(Plan=c(11,11,91,91),Price=c(4.4,4.4,NA,3.22,NA),factor=c(0.17,0.17,0.15,Type=c(4,4,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