-3

I have a Data Frame with a variable with different values for another variable. Like this: DataFrame

So, I need a subset when the value of S contain all the possible values of B. In this example, el subset is conformed by S = a and S = b:

Subset

Any idea? Thanks!!

  • I think you have a typo, it should be `S=a and S = d` (based on the image showed) – akrun May 24 '19 at 17:27
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can easily help with. That includes a sample of data (not pictures of it) and whatever code you've tried so far. Also is quite similar to https://stackoverflow.com/q/46964794/5325862 – camille May 24 '19 at 18:03
  • Reopened the dupe tag as it is not a duplicate of this post – akrun May 24 '19 at 22:56

1 Answers1

-1

An option would be to group by 'S' and filter the rows having all the unique values of the column 'B' %in% 'B'

library(dplyr)
un1 <- unique(df1$B)
df1 %>%
    group_by(S) %>%
    filter(all(un1 %in% B))
# A tibble: 8 x 2
# Groups:   S [2]
#  S         B
#  <fct> <dbl>
#1 a         1
#2 a         2
#3 a         3
#4 a         4
#5 d         1
#6 d         2
#7 d         3
#8 d         4

Or with data.table

library(data.table)
setDT(df1)[, .SD[all(un1 %in% B)], S]

Or using base R

df1[with(df1, ave(B, S, FUN = function(x) all(un1 %in% x)) == 1),]

data

df1 <- data.frame(S = rep(letters[1:4], c(4, 3, 2, 4)),
          B = c(1:4, c(1, 3, 4), 1:2, 1:4))
akrun
  • 874,273
  • 37
  • 540
  • 662