【发布时间】:2021-02-16 00:32:37
【问题描述】:
数据背景:人们在在线讨论板上回应以教他人
目标:根据他们是否在同一个帖子中轮流以及合作伙伴(双人)是谁来过滤数据。本质上,它归结为基于其他列的值进行过滤。
具体来说,我认为它会从检查 '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 ║
╚═════════╩═════════╩═══════════════╩════════════╝
【问题讨论】:
-
您可能想参考这篇文章以获取answer
-
谢谢@nyk,但我认为这是一个不同的问题,因为我们需要根据多个其他列的值进行过滤,而不是在同一列中。
-
这可能是因为您的过滤条件尚未确定。具体来说,您可能需要澄清“合作伙伴是谁”的要求。这是否意味着每个帖子 ID 都可以绑定到唯一的 dyad_id?但是您的输出似乎并非如此。您如何决定在每个帖子 ID 中保留哪个 dyad_id?