Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用 0 替换数据框中数字列中的所有 NA 值。我应该怎么做?
我得到了一个具有 NA 值的数值变量列表,如下所示(我确信它不是最优的):
iris[3,4] <- NA missingVars <- iris[colSums(is.na(iris)) > 0] missingNumVars <- missingVars[sapply(missingVars, is.numeric)]
这应该适合你
iris[is.na(iris)] <- 0
像这样的东西会起作用并避免相同大小的对象的重复副本iris(由创建is.na(iris))
iris
is.na(iris)
as.data.frame(lapply(iris, function(x) if(is.numeric(x)) {return(replace(x, is.na(x),0))} else {return(x)}) )