相关疑难解决方法(0)

删除缺少x%的列/行

我想删除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解决方案

r dplyr

17
推荐指数
3
解决办法
2万
查看次数

标签 统计

dplyr ×1

r ×1