|
| 1 | +#' Construct user-by-user square matrices of strengths of relation between |
| 2 | +#' users. |
| 3 | +#' |
| 4 | +#' @param user_data Result of `lapply(logins, repometrics_data_user)`. |
| 5 | +#' Contains the following fields: |
| 6 | +#' \enumerate{ |
| 7 | +#' \item general (not considered here) |
| 8 | +#' \item commit_cmt Comments on commits |
| 9 | +#' \item commits Commits to different repositories |
| 10 | +#' \item followers GitHub followers |
| 11 | +#' \item following Logins of people/orgs followed by user on GitHub |
| 12 | +#' \item issue_cmts Comments on issues |
| 13 | +#' \item issues Issues opened by user. |
| 14 | +#' } |
| 15 | +#' @return A `data.frame` of pairwise user logins, and proportions of overlap |
| 16 | +#' betwen repositories in the six variables described above. |
| 17 | +#' @noRd |
| 18 | +user_relation_matrices <- function (user_data) { |
| 19 | + |
| 20 | + user_names <- names (user_data) |
| 21 | + user_data <- add_user_login_cols (user_data) |> |
| 22 | + combine_user_data () |
| 23 | + |
| 24 | + # Pre-processing to name grouping column "repo" and count column "n": |
| 25 | + user_data$commit_cmt$repo <- |
| 26 | + paste0 (user_data$commit_cmt$org, user_data$commit_cmt$repo) |
| 27 | + |
| 28 | + user_data$followers <- |
| 29 | + dplyr::rename (user_data$followers, repo = followers) |> |
| 30 | + dplyr::mutate (n = 1L) |
| 31 | + user_data$following <- |
| 32 | + dplyr::rename (user_data$following, repo = following) |> |
| 33 | + dplyr::mutate (n = 1L) |
| 34 | + |
| 35 | + user_data$issue_cmts <- |
| 36 | + dplyr::rename (user_data$issue_cmts, repo = org_repo) |> |
| 37 | + dplyr::group_by (repo, login) |> |
| 38 | + dplyr::summarise (n = sum (num_comments), .groups = "keep") |
| 39 | + user_data$issues <- dplyr::rename (user_data$issues, repo = org_repo) |> |
| 40 | + dplyr::group_by (repo, login) |> |
| 41 | + dplyr::summarise (n = dplyr::n (), .groups = "keep") |
| 42 | + |
| 43 | + overlap <- lapply (names (user_data), function (n) { |
| 44 | + user_data [[n]] <- user_relate_fields (user_data, user_names, what = n) |
| 45 | + }) |
| 46 | + |
| 47 | + res <- dplyr::left_join (overlap [[1]], overlap [[2]], by = c ("login1", "login2")) |> |
| 48 | + dplyr::left_join (overlap [[3]], by = c ("login1", "login2")) |> |
| 49 | + dplyr::left_join (overlap [[4]], by = c ("login1", "login2")) |> |
| 50 | + dplyr::left_join (overlap [[5]], by = c ("login1", "login2")) |> |
| 51 | + dplyr::left_join (overlap [[6]], by = c ("login1", "login2")) |
| 52 | + |
| 53 | + return (res) |
| 54 | +} |
| 55 | + |
| 56 | +#' Add 'login' columns to all user data, so each element can be combined. |
| 57 | +#' @noRd |
| 58 | +add_user_login_cols <- function (user_data) { |
| 59 | + |
| 60 | + nms <- names (user_data) |
| 61 | + res <- lapply (seq_along (user_data), function (u) { |
| 62 | + nms_u <- names (user_data [[u]]) |
| 63 | + res_u <- lapply (seq_along (user_data [[u]]), function (i) { |
| 64 | + ud <- user_data [[u]] [[i]] |
| 65 | + if (is.data.frame (ud) && nrow (ud) > 0L) { |
| 66 | + ud$login <- names (user_data) [u] |
| 67 | + } else if (is.character (ud)) { |
| 68 | + ud <- data.frame (ud, login = names (user_data) [u]) |
| 69 | + names (ud) [1] <- names (user_data [[u]]) [i] |
| 70 | + } |
| 71 | + return (ud) |
| 72 | + }) |
| 73 | + names (res_u) <- nms_u |
| 74 | + |
| 75 | + return (res_u) |
| 76 | + }) |
| 77 | + names (res) <- nms |
| 78 | + |
| 79 | + return (res) |
| 80 | +} |
| 81 | + |
| 82 | +#' Combine all individual elements of 'user_data' for all users. |
| 83 | +#' |
| 84 | +#' The `add_user_login_cols` enables all data to be `rbind`-ed here. |
| 85 | +#' @noRd |
| 86 | +combine_user_data <- function (user_data) { |
| 87 | + |
| 88 | + data <- lapply (names (user_data [[1]]), function (n) { |
| 89 | + these <- lapply (user_data, function (i) i [[n]]) |
| 90 | + res <- do.call (rbind, these) |
| 91 | + rownames (res) <- NULL |
| 92 | + return (res) |
| 93 | + }) |
| 94 | + |
| 95 | + names (data) <- names (user_data [[1]]) |
| 96 | + data$general <- NULL |
| 97 | + |
| 98 | + return (data) |
| 99 | +} |
| 100 | + |
| 101 | +user_relate_fields <- function (user_data, user_names, what = "commits") { |
| 102 | + |
| 103 | + user_combs <- t (combn (user_names, m = 2L)) |
| 104 | + if (what == "commits") { |
| 105 | + user_data [[what]] <- dplyr::rename (user_data [[what]], n = num_commits) |
| 106 | + } else if (what == "commit_cmt") { |
| 107 | + user_data$commit_cmt$n <- 1L |
| 108 | + } |
| 109 | + |
| 110 | + res <- apply (user_combs, 1, function (i) { |
| 111 | + cmt1 <- dplyr::filter (user_data [[what]], login == i [1]) |> |
| 112 | + dplyr::group_by (repo) |> |
| 113 | + dplyr::summarise (n1 = sum (n)) |
| 114 | + cmt2 <- dplyr::filter (user_data [[what]], login == i [2]) |> |
| 115 | + dplyr::group_by (repo) |> |
| 116 | + dplyr::summarise (n2 = sum (n)) |
| 117 | + overlap <- dplyr::inner_join (cmt1, cmt2, by = "repo") |
| 118 | + |
| 119 | + res <- 0 |
| 120 | + if (nrow (overlap) > 0L) { |
| 121 | + res <- (sum (overlap$n1) + sum (overlap$n2)) / |
| 122 | + (sum (cmt1$n1) + sum (cmt2$n2)) |
| 123 | + } |
| 124 | + return (res) |
| 125 | + }) |
| 126 | + |
| 127 | + res <- data.frame ( |
| 128 | + login1 = user_combs [, 1], |
| 129 | + login2 = user_combs [, 2], |
| 130 | + res |
| 131 | + ) |
| 132 | + names (res) [3] <- what |
| 133 | + |
| 134 | + return (res) |
| 135 | +} |
0 commit comments