Search code examples
rgroup-byna

Remove rows with all NA values after groupby r


I want to remove rows which has all NAs after using group_by. here is a sample dataset:

df=data.frame(Col1=c("B","B","C","D",
                      "P1","P2","P3")
               ,Col2=c(NA,8,NA,9,10,8,9)
               ,Col3=c(NA,7,6,8,NA,7,8)
               ,Col4=c(NA,NA,7,7,NA,7,7))

i want to groupby Col1 and remove rows if column values are all NA. So the desired output is:

Col1 Col2 Col3 Col4
B 8 7 NA
C NA 6 7
D 9 8 7
P1 10 NA NA
P2 8 7 7
P3 9 8 7

any help would be really appreciated.


Solution

  • You don't need group_by, you can use if_any.

    library(dplyr)
    filter(df, if_any(-Col1, ~ !is.na(.)))
    #   Col1 Col2 Col3 Col4
    # 1    B    8    7   NA
    # 2    C   NA    6    7
    # 3    D    9    8    7
    # 4   P1   10   NA   NA
    # 5   P2    8    7    7
    # 6   P3    9    8    7