我已经学习了 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
空格已经被 NA 替换为 height$inches。 你也可以自己做:
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
关于r - 如何使用 r 编程语言处理数据集列中包含的空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58134241/