我已经尝试了几个小时,但我无法弄清楚。我有一个包含主题和条件的数据框df1
,我想从中排除具有特定值的观察值(变量“值”中小于 3)df2
。我无法使其工作,因为我需要从df1
不同级别的组合中删除的两个变量。
这是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"))
这是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"))
我想删除df1
所有主题和条件的组合,其值低于 3,所以这将是最终的 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"))
到目前为止,我一直在这样做,但我不能再这样做了,因为我有数百行......
df3 <- df1 %>% filter(!(subject==2 & condition=="A" |
subject==3 & (condition=="A" | condition=="B") |
subject==5 & condition=="C"))