填写缺失值

时间:2012-11-05 14:21:11

标签: r statistics missing-data

我有这样的数据集

 4  6 18 12  4  5
 2  9  0  3 NA 13
11 NA  6  7  7  9

如何使用R?

填充缺失值

1 个答案:

答案 0 :(得分:12)

如果您想用固定值替换您的NAs(a作为您的数据集):

a[is.na(a)] <- 0 #For instance

如果你想用一个值来替换它们,这个值是行号和列号的函数(正如你在评论中所建议的那样):

#This will replace them by the sum of their row number and their column number:
a[is.na(a)] <- rowSums(which(is.na(a), arr.ind=TRUE))

#This will replace them by their row number:
a[is.na(a)] <- which(is.na(a), arr.ind=TRUE)[,1]

#And this by their column number:
a[is.na(a)] <- which(is.na(a), arr.ind=TRUE)[,2]