Q:

R: group by a data set, with condition that all values in a particular column in the group match some requirement

Got a data set like:
A B
a x
a y
b z
b z
c x
c z

Now I would like to group by A, with values in B in each group contains 'x' or 'y' only. e.g only 'c' would be chosen.
Any simple function for this solution? Please help

A:

We can select groups where all the values are either "x" or "y". 
In base R, we can use ave with subset
unique(subset(df, ave(B %in% c('x', 'y'), A, FUN =  all), select = A))

#  A
#1 a
#8 d

Or with dplyr : 
library(dplyr)

df %>%
  group_by(A) %>%
  filter(all(B %in% c('x', 'y'))) %>%
  distinct(A)

data
df <- data.frame(A = c('a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'), 
                 B = c('x', 'x', 'y', 'z', 'z', 'x', 'z', 'x', 'x'))

