我想删除NA数据框中超过50%s的所有列或行.
这是我的解决方案:
# delete columns with more than 50% missings
miss <- c()
for(i in 1:ncol(data)) {
if(length(which(is.na(data[,i]))) > 0.5*nrow(data)) miss <- append(miss,i)
}
data2 <- data[,-miss]
# delete rows with more than 50% percent missing
miss2 <- c()
for(i in 1:nrow(data)) {
if(length(which(is.na(data[i,]))) > 0.5*ncol(data)) miss2 <- append(miss2,i)
}
data <- data[-miss,]
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一个更好/更快的解决方案.
我也很感激dplyr解决方案