我已经学习了r中NA值的插补,我们通常会求出数据的平均值(如果是数值的话),并将其放在特定列的NA位置但我想问,如果不是na,而是空的,即单元格没有任何列,我该怎么办。
请帮帮我。
最佳答案
让我们从一些测试数据开始:
person_id <- c("1","2","3","4","5","6","7","8","9","10")
inches <- as.numeric(c("56","58","60","62","64","","68","70","72","74"))
height <- data.frame(person_id,inches)
height
person_id inches
1 1 56
2 2 58
3 3 60
4 4 62
5 5 64
6 6 NA
7 7 68
8 8 70
9 9 72
10 10 74
空白处已经换成了高$英寸的钠。
你也可以自己做:
height$inches[height$inches==""] <- NA
现在用不缺少的英寸值的平均值填写
NA
。options(digits=4)
height$inches[is.na(height$inches)] <- mean(height$inches,na.rm=T)
height
person_id inches
1 1 56.00
2 2 58.00
3 3 60.00
4 4 62.00
5 5 64.00
6 6 64.89
7 7 68.00
8 8 70.00
9 9 72.00
10 10 74.00