How to remove only rows that have all NA in R? [duplicate]

旧巷老猫 提交于 2019-12-02 17:16:28

问题


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()

回答1:


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","","","") 



回答2:


> df[rowSums(df=="")!=ncol(df), ]
    x1   x2   x3   x4
1  Bob  Bob  Bob  Bob
2 Mary Mary Mary Mary
4 Jane Jane Jane Jane


来源:https://stackoverflow.com/questions/51214357/how-to-remove-only-rows-that-have-all-na-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!