根据 R 中多个其他列的值进行过滤

Filtering based on the values of multiple other columns in R

数据上下文:人们在在线讨论板上回应教导他人

目标:根据他们是否在同一个 post 中轮流以及合作伙伴 (dyad) 是谁来过滤数据。本质上,它归结为基于其他列的值进行过滤。

具体来说,我认为它会从检查是否'turntaking'==1开始,然后在相同的'post_id'中保持相同的'dyad_id'的观察。我在如何按多个条件进行过滤时遇到问题。

示例数据:

structure(list(post_id = c(100, 230, 100, 100, 100, 100), dyad_id = structure(c(2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("42_27", "53_27"), class = "factor"), 
    dyad_id_order = structure(c(4L, 4L, 2L, 3L, 1L, 3L), .Label = c("27_42", 
    "27_53", "42_27", "53_27"), class = "factor"), turntaking = c(0, 
    0, 1, 0, 1, 1)), class = "data.frame", row.names = c(NA, 
-6L), variable.labels = structure(character(0), .Names = character(0)), codepage = 65001L)

示例数据看起来像:

╔═════════╦═════════╦═══════════════╦════════════╦══════════════════════════════════════════════════════════╗
║ post_id ║ dyad_id ║ dyad_id_order ║ turntaking ║ (note)                                                   ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   100   ║  53_27  ║     53_27     ║      0     ║ Keep                                                     ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   230   ║  53_27  ║     53_27     ║      0     ║ Drop                                                     ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   100   ║  53_27  ║     27_53     ║      1     ║ Keep: ID27 responded to ID53's response in the first row ║
║         ║         ║               ║            ║ (They are both found under the same post_id)             ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   100   ║  42_27  ║     42_27     ║      0     ║ Keep                                                     ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   100   ║  42_27  ║     27_42     ║      1     ║ Keep                                                     ║
╠═════════╬═════════╬═══════════════╬════════════╬══════════════════════════════════════════════════════════╣
║   100   ║  42_27  ║     42_27     ║      1     ║ Keep                                                     ║
╚═════════╩═════════╩═══════════════╩════════════╩══════════════════════════════════════════════════════════╝

最终的输出看起来像:

╔═════════╦═════════╦═══════════════╦════════════╗
║ post_id ║ dyad_id ║ dyad_id_order ║ turntaking ║
╠═════════╬═════════╬═══════════════╬════════════╣
║   100   ║  53_27  ║     53_27     ║      0     ║
╠═════════╬═════════╬═══════════════╬════════════╣
║   100   ║  53_27  ║     27_53     ║      1     ║
╠═════════╬═════════╬═══════════════╬════════════╣
║   100   ║  42_27  ║     42_27     ║      0     ║
╠═════════╬═════════╬═══════════════╬════════════╣
║   100   ║  42_27  ║     27_42     ║      1     ║
╠═════════╬═════════╬═══════════════╬════════════╣
║   100   ║  42_27  ║     42_27     ║      1     ║
╚═════════╩═════════╩═══════════════╩════════════╝

这会查看每个 post_id-dyad_id 组合,只保留那些在某个时候有轮换标志的组合。

  my_data %>%
    group_by(post_id, dyad_id) %>%
    filter(sum(turntaking) > 0) %>%
    ungroup()

# A tibble: 5 x 4
  post_id dyad_id dyad_id_order turntaking
    <dbl> <fct>   <fct>              <dbl>
1     100 53_27   53_27                  0
2     100 53_27   27_53                  1
3     100 42_27   42_27                  0
4     100 42_27   27_42                  1
5     100 42_27   42_27                  1