I have a dataframe which contains three columns, and a second which contains two columns.
df1 <- data.frame(X1 = c('A', 'A', 'A', 'A', 'A', 'A', 'B'),
X2 = c('B', 'B', 'B', 'C', 'C', 'D', 'C'),
X3 = c('C', 'D', 'E', 'D', 'E', 'E', 'D'))
df2 <- data.frame(X1 = c('A', 'A'),
X2 = c('B', 'D'))
Questions:
- How do I find the rows in
df1which contain all the elements of a row ofdf2? i.e. rows 1:3 ofdf1contain bothAandB(first row ofdf2). I am looking to remove any rows ofdf1which contain both elements of the rows ofdf2. So in the example, I would like to remove rows 1, 2, 3, 4 and 6 ofdf1as these includeAandBORAandD. - Is there a quick way to count the number of rows for each row of
df2without looping? i.e.df2row 1 would have a count of 3 and row 2 a count of 3.