gpt4 book ai didi

r - 填充缺失值

转载 作者:行者123 更新时间:2023-12-04 22:22:30 25 4
gpt4 key购买 nike

我有一个这样的数据集

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com