1

I have the following data frame

filen<-c('510-1','510-2','510-2','510-2','510-3','510-3','510-4')
disp<-c('g','ng','ng','ng','g','ng','ng')

df<-data.frame(filen,disp)


  filen disp
1 510-1    g
2 510-2   ng
3 510-2   ng
4 510-2   ng
5 510-3    g
6 510-3   ng
7 510-4   ng

Basically I want to isolate the file numbers where ng is the only type of disp associated with that filen. So that I get a dataset like this. How do I do this using dplyr

filen disp
510-2  ng
510-4  ng
Nathan123
  • 763
  • 5
  • 18
  • 1
    Pretty similar to [here](https://stackoverflow.com/q/53705012/5325862) and [here](https://stackoverflow.com/q/43689057/5325862) – camille Jul 08 '19 at 15:45
  • 2
    @akrun I don't think the question is an exact dupe of those, which is why I didn't vote to close, but I do think they are similar enough to help the OP and future users of the site. That doesn't have anything to do with you answering, beyond the fact that your nearly identical answers on those posts are what I'm saying would help the OP – camille Jul 08 '19 at 16:24

2 Answers2

3

We can group by 'filen', filter the groups where all the 'disp' values are 'ng' and get the distinct rows

library(dplyr)
df %>%
   group_by(filen) %>%
   filter( all(disp == 'ng')) %>%
   distinct
# A tibble: 2 x 2
# Groups:   filen [2]
#  filen disp 
#   <fct> <fct>
#1 510-2 ng   
#2 510-4 ng   

Or

df %>% 
   distinct %>%
   group_by(filen) %>%
   filter(n_distinct(disp) == 1, disp == 'ng')

Or we can use data.table

library(data.table)
setDT(unique(df))[,  .SD[uniqueN(disp)==1 & disp == "ng"], filen]
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Using base R, we one option can be to calculate the frequency of df using table, find the filen which have ng value greater than 0 and g value equal to 0 and keep only unique rows.

df1 <- as.data.frame.matrix(table(df))
unique(df[df$filen %in% rownames(df1)[df1$ng > 0 & df1$g == 0], ])

#  filen disp
#2 510-2   ng
#7 510-4   ng

Or with ave

unique(df[ave(df$disp == "ng", df$filen, FUN = all), ])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213