我有以下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
。对于数字列,我想将其替换为mean
,对于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)
}
答案 0 :(得分:5)
使用基本方法,您可以编写如下函数:
myFun <- function(x) {
if (is.numeric(x)) {
x[is.na(x)] <- mean(x, na.rm = TRUE)
x
} else {
x[is.na(x)] <- names(which.max(table(x)))
x
}
}
...并将其应用于:
x[, lapply(.SD, myFun)]
# id1 id2
# 1: a 2.0
# 2: a 3.0
# 3: a 3.4
# 4: b 3.0
# 5: b 4.0
# 6: a 5.0
请注意,如果存在关联,which.max
将采用第一大值。
我想它也可以写成:
myFun <- function(inDT) {
for (i in 1:ncol(inDT)) {
temp <- unlist(inDT[, i, with = FALSE], use.names = FALSE)
set(inDT, which(is.na(temp)), i,
if (is.numeric(temp)) {
mean(temp, na.rm = TRUE)
} else {
names(which.max(table(temp)))
} )
}
inDT
}
y <- copy(x)
myFun(y)
# id1 id2
# 1: a 2.0
# 2: a 3.0
# 3: a 3.4
# 4: b 3.0
# 5: b 4.0
# 6: a 5.0