Judith
Judith

Reputation: 167

Remove groups which do not have non-consecutive NA values in R

I have the following Data frame

group <- c(2,2,2,2,4,4,4,4,5,5,5,5)
D <- c(NA,2,NA,NA,NA,2,3,NA,NA,NA,1,1)
df <- data.frame(group, D)
df
   group  D
1      2 NA
2      2  2
3      2 NA
4      2 NA
5      4 NA
6      4  2
7      4  3
8      4 NA
9      5 NA
10     5 NA
11     5  1
12     5  1

I would like to only keep groups that contain non consecutive NA values at least once. in this case group 5 would be removed because it does not contain non consecutive NA values, but only consecutive NA values. group 2 and 4 remain because they do contain non consecutive NA values (NA values separated by row(s) with a non NA value).

therefore the resulting data frame would look like this:

df2
  group  D
1     2 NA
2     2  2
3     2 NA
4     2 NA
5     4 NA
6     4  2
7     4  3
8     4 NA

any ideas :)?

Upvotes: 1

Views: 141

Answers (1)

talat
talat

Reputation: 70336

How about using difference between the index of NA-values per group?

library(dplyr)
df %>% group_by(group) %>% filter(any(diff(which(is.na(D))) > 1))

## A tibble: 8 x 2
## Groups:   group [2]
#  group     D
#  <dbl> <dbl>
#1    2.   NA 
#2    2.    2.
#3    2.   NA 
#4    2.   NA 
#5    4.   NA 
#6    4.    2.
#7    4.    3.
#8    4.   NA 

I'm not sure this would catch all potential edge cases but it seems to work for the given example.

Upvotes: 1

Related Questions