我有以下的
data.table
。x = structure(list(id1 = c("a", "a", "a", "b", "b", NA), id2 = c(2, 3, NA,3, 4, 5)), .Names = c("id1", "id2"), row.names = c(NA, -6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x1fe4a78>)
我试图用不同的策略替换每列中的NA
。对于数字列,我想用平均值
来替换它,对于factor
或character
列,我想用最常见的值来替换它。我尝试了以下方法,但它什么也没做。
for (j in 1:ncol(x)){
if(is.numeric(unlist(x[,j,with=FALSE]))){
m = mean(unlist(x[,j,with=FALSE]))
set(x,which(is.na(x[[j]])),j,m)
}else{
m = sort(table(x),decreasing=TRUE)[[1]]
set(x,which(is.na(x[[j]])),j,m)
}