Reputation: 119
Here is my example:
mydf <- data.frame('col_1'=c('A','A','B','B','C','C','D','D'), 'col_2'=c(100,NA,90,30,50,60,10,NA))
col_1 col_2
1 A 100
2 A NA
3 B 90
4 B 30
5 C 50
6 C 60
7 D 10
8 D NA
I would like to remove the NA values, grouped by col_1
, to produce the result below.
col_1 col_2
1 B 90
2 B 30
3 C 50
4 C 60
How can I do it?
Upvotes: 5
Views: 3804
Reputation: 887951
We can group by 'col_1' and then filter
the 'col_2' that have no 'NA's
library(dplyr)
mydf %>%
group_by(col_1) %>%
filter(!any(is.na(col_2)))
Or do this with all
mydf %>%
group_by(col_1) %>%
filter(all(!is.na(col_2)))
Or this can be done with base R
subset(mydf, col_1 %in% names(which(!rowsum(+(is.na(col_2)),
group = col_1)[,1])))
Upvotes: 3