如果我想用我的数据框的平均值替换所有负值以及(999,9991,9992,9996)的值,我应该怎么做。我尝试了以下但没有工作
x<-c(-1,-2,0,1,9991)
y<-c(1,2,3,4,999)
z<-c(100,101,-999,9992,9996)
data<-as.data.frame(cbind(x,y,z))
# replacing special values with missing first
data[] <- lapply(data, function(x){
x[x < 0 | x %in% c(999, 9991, 9992, 9996)] <- NA
return(x)
})
# and now replacing missing values with mean of column
data[] <- lapply(data, function(x){
x[is.na(x)]<- mean(x)
return(x)
})
但是当我在R中运行它时它不会替换缺失值