R 用唯一的随机数替换 NA

R Replacing NAs with a unique random numer

我在数据框中有一个看起来像这样的变量

x=c(1,2,4,6,7,NA,NA,5,NA,NA,9)

x 中的每个元素都是唯一编号,我想用唯一编号替换 NA。

我试过的是这样的,但想知道是否有更有效的方法来做到这一点。

x[is.na(x)]=sample(10:15,replace=F)
Warning message:
In x[is.na(x)] = sample(10:15, replace = F) :
  number of items to replace is not a multiple of replacement length

谢谢!

您可以遍历并创建一个缺失值索引向量,然后将该向量传递给 replace() 并在其中嵌套 random() 以生成您要用其替换缺失值的随机数.

# data
x=c(1,2,4,6,7,NA,NA,5,NA,NA,9)
# vector of missing values
v <- NULL
# loop to find missing value indices
for(i in 1:length(x)){
  if(is.na(x[i])==TRUE)
    v <- append(v, i)
}
# replace missing values with a random integer
xnew <- replace(x, v, sample(10, length(v), replace = FALSE))



x
>> 1  2  4  6  7 NA NA  5 NA NA  9
xnew
>> 1  2  4  6  7  5 10  5  4  2  9

如果您 "count" 从您的候选值集中抽样的项目数( is.na 的总和似乎是一个很好的计数方法),那么您将不会得到错误:

x[is.na(x)] <- sample(10:15, size=sum(is.na(x)), replace=F)

> x
 [1]  1  2  4  6  7 12 14  5 11 13  9