如何只删除R中所有NA的行? [英] How to remove only rows that have all NA in R?
本文介绍了如何只删除R中所有NA的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据帧,其中只有一些行具有所有NA. 如何遍历并删除所有这些行? 我尝试了na.omit(),但是没有用. 在此示例中,我需要删除第3行和第5行
I have a data frame where only some of the rows have all NA. How do I loop through and delete all these rows? I tried na.omit() but that doesn't work. In this example I need rows 3 and 5 removed
x1 <- c("Bob", "Mary","","Jane","")
x2 <- c("Bob","Mary","","Jane","")
x3 <- c("Bob", "Mary","","Jane","")
x4 <- c("Bob","Mary","","Jane","")
df <- data.frame(x1,x2,x3,x4)
df <- df %>% na.omit()
推荐答案
这里是一种选择;首先,您需要定义NA模式.
Here is one option; first you need to define the NA pattern.
df[df == ""] <- NA # define NA pattern
df[rowSums(is.na(df)) != ncol(df), ] # result
# Try with x1 <- c("Bob", "Mary","","","")
这篇关于如何只删除R中所有NA的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文