聚合合作伙伴之间的交互模式

Aggregating interaction patterns between partners

研究背景:'Listener' post 他们的想法和 'speaker' 对听众的反应。每个线程的第一个观察到的侦听器是原始 post 编写器。

示例数据:

structure(list(topic = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2), thread = c(1, 
1, 1, 2, 2, 2, 3, 3, 3, 3, 3), listener_id = c(111, 111, 333, 
222, 222, 222, 444, 444, 777, 444, 444), speaker_id = c(222, 
333, 111, 111, 555, 444, 222, 777, 444, 333, 777), speaker_response = c(3, 
3, 6, 1, 4, 4, 4, 2, 2, 5, 3)), class = "data.frame", row.names = c(NA, 
-11L), codepage = 65001L)

在 table 中,它看起来像:

╔═══════╦════════╦═════════════╦════════════╦══════════════════╗
║ topic ║ thread ║ listener_id ║ speaker_id ║ speaker_response ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    1   ║     111     ║     222    ║         3        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    1   ║     111     ║     333    ║         3        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    1   ║     333     ║     111    ║         6        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    2   ║     222     ║     111    ║         1        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    2   ║     222     ║     555    ║         4        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  1.00 ║    2   ║     222     ║     444    ║         4        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  2.00 ║    3   ║     444     ║     222    ║         4        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  2.00 ║    3   ║     444     ║     777    ║         2        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║ 2.00  ║    3   ║     777     ║     444    ║         2        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║ 2.00  ║    3   ║     444     ║     333    ║         5        ║
╠═══════╬════════╬═════════════╬════════════╬══════════════════╣
║  2.00 ║    3   ║     111     ║     777    ║         3        ║
╚═══════╩════════╩═════════════╩════════════╩══════════════════╝

目标是:

这在文字上可能会造成混淆; 可视化,最终结果看起来像:

╔═══════╦════════╦═════════════╦═════════════════╦══════════════════════════════════════════╗
║ topic ║ thread ║ listener_id ║ others_response ║                explanation               ║
╠═══════╬════════╬═════════════╬═════════════════╬══════════════════════════════════════════╣
║   1   ║    1   ║     111     ║        3        ║ Here, the first listener was 111         ║
║       ║        ║             ║                 ║ and others' average response = (3+3)/2,  ║
║       ║        ║             ║                 ║ excluding the last observation where 111 ║
║       ║        ║             ║                 ║ was the speaker.                         ║
╠═══════╬════════╬═════════════╬═════════════════╬══════════════════════════════════════════╣
║   1   ║    2   ║     222     ║        3        ║                                          ║
╠═══════╬════════╬═════════════╬═════════════════╬══════════════════════════════════════════╣
║   2   ║    3   ║     444     ║       3.5       ║ Excluding the response made by the first ║
║       ║        ║             ║                 ║ listener, the average is (4+2+5+3)/4.    ║
╚═══════╩════════╩═════════════╩═════════════════╩══════════════════════════════════════════╝

您可以删除那些 speaker_id 等于 first listener_idmean 每个 topicspeaker_response 值的响应thread.

library(dplyr)

df %>%
  group_by(topic, thread) %>%
  summarise(others_response = mean(speaker_response[speaker_id != first(listener_id)]), 
            listener_id = first(listener_id))

#  topic thread others_response listener_id
#  <dbl>  <dbl>           <dbl>       <dbl>
#1     1      1             3           111
#2     1      2             3           222
#3     2      3             3.5         444