I have been trying for hours and I can't figure it out. I have a data frame with subjects and conditions df1
, from which I want to exclude observations which have a certain value (less than 3 in the variable "value" from df2
. I cannot make it work because I need to remove from df1
, combinations of different levels of two variables.
This is df1:
df1 <- structure(list(subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
condition = c("A", "A", "A", "B", "B", "B", "C", "C","C", "A", "A",
"A", "B", "B", "B", "C", "C", "C", "A", "A", "A","B", "B", "B", "C", "C", "C")),
row.names = c(NA, -27L), class = c("tbl_df", "tbl", "data.frame"))
And this is df2
df2 <- structure(list(subject = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,4L, 4L, 4L, 5L, 5L, 5L),
condition = c("A", "B", "C", "A", "B","C", "A", "B", "C", "A", "B", "C", "A", "B", "C"),
value = c(10L, 8L, 7L, 3L, 8L, 5L, 3L, 3L, 9L, 8L, 7L, 8L, 10L, 6L, 2L)),
row.names = c(NA,-15L), class = c("tbl_df", "tbl", "data.frame"))
And I want to remove in df1
all the combinations of subject and condition with a value under 3 so this would be the final df:
df3 <- structure(list(subject = c(2L, 3L, 3L, 5L),
condition = c("A","A", "B", "C")),
row.names = c(NA, -4L),
class = c("tbl_df","tbl", "data.frame"))
So far I have been doing it like this, but I can't anymore because I have hundreds of rows...
df3 <- df1 %>% filter(!(subject==2 & condition=="A" |
subject==3 & (condition=="A" | condition=="B") |
subject==5 & condition=="C"))