Home > Enterprise >  Omit groups if they meet a certain condition in R
Omit groups if they meet a certain condition in R

Time:04-06

My data are as follows:

df <- structure(list(year = c(2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 
2020L, 2020L), site = c("A", "B", "C", "D", "A", "B", "C", "D"
), value = c(200L, 100L, 50L, NA, 300L, NA, NA, NA), dist = c(10L, 
15L, 30L, 36L, 10L, 15L, 30L, 36L)), class = "data.frame", row.names = c(NA, 
-8L))

I would like to omit an entire group (the year) if there is greater than one NA value within the group.

In the data example I provide, the outcome would be a dataframe with only 2019 observations (though my real data have many more years).

Thank you in advance!

CodePudding user response:

Try:

library(dplyr)
df %>% group_by(year) %>% filter(sum(is.na(value))<2)

# year site  value  dist
# <int> <chr> <int> <int>
#   1  2019 A       200    10
# 2  2019 B       100    15
# 3  2019 C        50    30
# 4  2019 D        NA    36
  •  Tags:  
  • r
  • Related