4

我有一个这样的数据集

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

如何使用 R 填充缺失值?

4

1 回答 1

12

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

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]
于 2012-11-05T14:41:29.240 回答