Keeping all NAs in dplyr distinct function

Viewed 388

I have a data.frame (the eBird basic dataset) where many observers may upload a record from a same sighting to a database, in this case, the event is given a "group identifier"; when not from a group session, a NA will appear in the database; so I'm trying to filter out all those duplicates from group events and keep all NAs, I'm trying to do this without splitting the dataframe in two:

library(dplyr)
set.seed(1)
df <- tibble(
  x = sample(c(1:6, NA), 30, replace = T),
  y = sample(c(letters[1:4]), 30, replace = T)
)

df %>% count(x,y)

gives:

> df %>% count(x,y)
# A tibble: 20 x 3
       x y         n
   <int> <chr> <int>
 1     1 a         1
 2     1 b         2
 3     2 a         1
 4     2 b         1
 5     2 c         1
 6     2 d         3
 7     3 a         1
 8     3 b         1
 9     3 c         4
10     4 d         1
11     5 a         1
12     5 b         2
13     5 c         1
14     5 d         1
15     6 a         1
16     6 c         2
17    NA a         1
18    NA b         2
19    NA c         2
20    NA d         1

I want no NA at x to be grouped together, as here happened with "NA b" and "NA c" combinations; distinct function has no information on not taking NAs into the computation; is splitting the dataframe the only solution?

1 Answers

With distinct an option is to create a new column based on the NA elements in 'x'

library(dplyr)
df  %>%
    mutate(x1 = row_number() * is.na(x)) %>%
    distinct %>% 
    select(-x1)

Or we can use duplicated with an OR (|) condition to return all NA elements in 'x' with filter

df %>%
    filter(is.na(x)|!duplicated(cur_data()))
# A tibble: 20 x 2
#       x y    
#   <int> <chr>
# 1     1 b    
# 2     4 b    
# 3    NA a    
# 4     1 d    
# 5     2 c    
# 6     5 a    
# 7    NA d    
# 8     3 c    
# 9     6 b    
#10     2 b    
#11     3 b    
#12     1 c    
#13     5 d    
#14     2 d    
#15     6 d    
#16     2 a    
#17    NA c    
#18    NA a    
#19     1 a    
#20     5 b    
 
Related