Counting instances of "indirect" interactions between partners

Research context: speakers (writers) and recipients interact in written communication concerning a certain discussion topic . The first speaker is the original person who posted a thread .

Data look like:

structure(list(topic = c(1, 1, 1, 1, 1, 1, 2, 2), thread = c(1, 
1, 1, 2, 2, 2, 3, 3), speaker_id = c(111, 111, 111, 222, 222, 
222, 111, 222), recipient_id = c(222, 333, 444, 111, 555, 444, 
222, 111), dyad = structure(c(1L, 2L, 3L, 1L, 5L, 4L, 1L, 1L), .Label = c("111_222", 
"111_333", "111_444", "222_444", "222_555"), class = "factor")), class = "data.frame", row.names = c(NA, 
-8L), codepage = 65001L)

The aim is to create a variable:

threads_present : In how many threads--other than the given thread--within a discussion topic were the speaker and recipient present as recipients, without being partnered (or forming a dyad)?

The results would look like:

╔═══════╦════════╦═════════╦═══════════╦═════════╦═════════╦════════════════════════════════════════════╗
║ topic ║ thread ║ speaker ║ recipient ║   dyad  ║ threads ║                    note                    ║
║       ║        ║    id   ║     id    ║         ║ present ║                                            ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    1   ║   111   ║    222    ║ 111_222 ║    0    ║ Outside the given thread (thread #1) of    ║
║       ║        ║         ║           ║         ║         ║ the given topic (topic #1), 111 and 222    ║
║       ║        ║         ║           ║         ║         ║ are not found together as recipients       ║
║       ║        ║         ║           ║         ║         ║ other than being in a dyad                 ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    1   ║   111   ║    333    ║ 111_333 ║    0    ║                                            ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    1   ║   111   ║    444    ║ 111_444 ║    1    ║ 111 and 444 are found in thread #2,        ║
║       ║        ║         ║           ║         ║         ║ where they did not interact (made a dyad), ║
║       ║        ║         ║           ║         ║         ║ but were only recipients of                ║
║       ║        ║         ║           ║         ║         ║ the original speaker (111)                 ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    2   ║   222   ║    111    ║ 111_222 ║    0    ║                                            ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    2   ║   222   ║    555    ║ 222_555 ║    0    ║                                            ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  1.00 ║    2   ║   222   ║    444    ║ 222_444 ║    1    ║ 222 and 444 are found together             ║
║       ║        ║         ║           ║         ║         ║ in thread #1, where they did not           ║
║       ║        ║         ║           ║         ║         ║ interact                                   ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  2.00 ║    3   ║   111   ║    222    ║ 111_222 ║    0    ║                                            ║
╠═══════╬════════╬═════════╬═══════════╬═════════╬═════════╬════════════════════════════════════════════╣
║  2.00 ║    3   ║   222   ║    111    ║ 111_222 ║    0    ║                                            ║
╚═══════╩════════╩═════════╩═══════════╩═════════╩═════════╩════════════════════════════════════════════╝
library(tidyverse)

exdf <- tibble::tribble(
  ~topic, ~thread, ~speaker_id, ~recipient_id,     ~dyad,
  1,       1,         111,           222, "111_222",
  1,       1,         111,           333, "111_333",
  1,       1,         111,           444, "111_444",
  1,       2,         222,           111, "111_222",
  1,       2,         222,           555, "222_555",
  1,       2,         222,           444, "222_444",
  2,       3,         111,           222, "111_222",
  2,       3,         222,           111, "111_222"
)

(exdf_base <- mutate(rowwise(exdf),
  conv_id = paste(topic, thread, sep = "@")
))

find_in_other_convs <- function(cid, r1, r2) {
  filter(
    exdf_base,
    recipient_id %in% c(r1, r2),
    conv_id != cid,
    !speaker_id %in% c(r1, r2)
  ) %>%
    group_by(conv_id) %>%
    summarise(n = n()) %>%
    filter(n == 2) %>%
    pull(conv_id) -> other_convs
  num_of_other_convs <- length(other_convs)
  list(
    other_convs = other_convs,
    num_of_other_convs = num_of_other_convs
  )
}

(result_df <- mutate(rowwise(exdf_base),
  packed_metrics = list(find_in_other_convs(
    conv_id,
    speaker_id,
    recipient_id
  ))
) %>% unnest_wider(packed_metrics))
# A tibble: 8 x 8
  topic thread speaker_id recipient_id dyad    conv_id num_of_other_convs other_convs
  <dbl>  <dbl>      <dbl>        <dbl> <chr>   <chr>                <int> <chr>      
1     1      1        111          222 111_222 1@1                      0 NA         
2     1      1        111          333 111_333 1@1                      0 NA         
3     1      1        111          444 111_444 1@1                      1 1@2        
4     1      2        222          111 111_222 1@2                      0 NA         
5     1      2        222          555 222_555 1@2                      0 NA         
6     1      2        222          444 222_444 1@2                      1 1@1        
7     2      3        111          222 111_222 2@3                      0 NA         
8     2      3        222          111 111_222 2@3                      0 NA

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.