r - 填充缺失值

标签 r statistics missing-data

我有一个这样的数据集

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

如何使用 R 填充缺失值?

最佳答案

如果你想用固定值替换你的 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]

关于r - 填充缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234005/

相关文章:

r - 如何在 R/ggplot2 中创建数据的阶梯图?

r - 如何在同一图上叠加 2 条正态分布曲线?

python - 计算德州扑克中 5 张牌的抽牌次数

Gnuplot:当日期序列中缺少数据时如何默认为 '0'

python - 计算pandas DataFrame中缺失值行数的最佳方法

雷达图外围标签截止

r - 当字符串包含特殊字符时检查一个字符串是否包含R中的另一个字符串

r - 在 R 中,当从另一列中的分隔文本创建列名称时,新列名称仅从第一行分配

algorithm - 我如何计算准确度?

r - 找到R中NA值最多的行