I want to delete all columns or rows with more than 50% NA
s in a data frame.
This is my solution:
# 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,]
but I'm looking for a nicer/faster solution.
I would also appreciate a dplyr
solution
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…