Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows with all NA values after groupby r

Tags:

r

group-by

na

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.

like image 318
krishthw Avatar asked Jan 25 '26 14:01

krishthw


2 Answers

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
like image 74
r2evans Avatar answered Jan 27 '26 03:01

r2evans


There is no need for group-by, keep only rows where there is at least 1 non-na column, excluding Col1:

df[ rowSums(!is.na(df[, -1])) > 0, ]
#   Col1 Col2 Col3 Col4
# 2    B    8    7   NA
# 3    C   NA    6    7
# 4    D    9    8    7
# 5   P1   10   NA   NA
# 6   P2    8    7    7
# 7   P3    9    8    7
like image 23
zx8754 Avatar answered Jan 27 '26 02:01

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!