数据背景:人们在网上讨论区回应教别人
:根据他们是否在同一帖子中轮流以及合作伙伴(双)是谁来过滤数据。从本质上讲,它可以归结为基于其他列的值进行过滤。
具体来说,我认为它将从检查"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