Counting # of distinct "connections" of names

Hi everyone, I'm going a bit nuts right now, as I'm about 99% sure I've done something like this before -- but how would I go about finding the # of distinct players to which a player is "connected"?

Here's an example of the data:

# A tibble: 30 x 2
   goal             primary_assist  
   <chr>            <chr>           
 1 Jesper Fast      Filip Chytil    
 2 Pavel Buchnevich Mika Zibanejad  
 3 Brett Howden     Adam McQuaid    
 4 Jimmy Vesey      Kevin Hayes     
 5 Chris Kreider    Brett Howden    
 6 Jimmy Vesey      Brady Skjei     
 7 Pavel Buchnevich Mats Zuccarello 
 8 Chris Kreider    Anthony DeAngelo
 9 Brett Howden     Mats Zuccarello 
10 Brendan Smith    Pavel Buchnevich
# ... with 20 more rows

So you see Jesper Fast there? He has 1 goal where the accompanying primary assist is Filip Chytil, and 1 primary assist where the accompanying goal is Kevin Hayes. Because of that, he's "connected" to 2 distinct players.

Any idea how I'd calculate this for all players? I'm pretty sure this is a tidyr-type problem. I'd really appreciate any insight.

mydata <- structure(list(goal = c("Jesper Fast", "Pavel Buchnevich", "Brett Howden", 
"Jimmy Vesey", "Chris Kreider", "Jimmy Vesey", "Pavel Buchnevich", 
"Chris Kreider", "Brett Howden", "Brendan Smith", "Brady Skjei", 
"Mika Zibanejad", "Chris Kreider", "Kevin Hayes", "Mika Zibanejad", 
"Jimmy Vesey", "Chris Kreider", "Mika Zibanejad", "Mika Zibanejad", 
"Mats Zuccarello", "Mika Zibanejad", "Mats Zuccarello", "Kevin Hayes", 
"Pavel Buchnevich", "Vladislav Namestnikov", "Anthony DeAngelo", 
"Ryan Spooner", "Mats Zuccarello", "Chris Kreider", "Chris Kreider"
), primary_assist = c("Filip Chytil", "Mika Zibanejad", "Adam McQuaid", 
"Kevin Hayes", "Brett Howden", "Brady Skjei", "Mats Zuccarello", 
"Anthony DeAngelo", "Mats Zuccarello", "Pavel Buchnevich", NA, 
"Marc Staal", "Kevin Shattenkirk", "Mats Zuccarello", "Chris Kreider", 
"Neal Pionk", "Neal Pionk", "Neal Pionk", "Adam McQuaid", "Neal Pionk", 
"Neal Pionk", "Mika Zibanejad", "Jesper Fast", "Brendan Smith", 
"Pavel Buchnevich", "Vladislav Namestnikov", "Anthony DeAngelo", 
"Kevin Shattenkirk", "Brady Skjei", "Mats Zuccarello")), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("goal", 
"primary_assist"))

Are you looking for something like this ?

mydata
mydata <- structure(list(goal = c("Jesper Fast", "Pavel Buchnevich", "Brett Howden", 
                                  "Jimmy Vesey", "Chris Kreider", "Jimmy Vesey", "Pavel Buchnevich", 
                                  "Chris Kreider", "Brett Howden", "Brendan Smith", "Brady Skjei", 
                                  "Mika Zibanejad", "Chris Kreider", "Kevin Hayes", "Mika Zibanejad", 
                                  "Jimmy Vesey", "Chris Kreider", "Mika Zibanejad", "Mika Zibanejad", 
                                  "Mats Zuccarello", "Mika Zibanejad", "Mats Zuccarello", "Kevin Hayes", 
                                  "Pavel Buchnevich", "Vladislav Namestnikov", "Anthony DeAngelo", 
                                  "Ryan Spooner", "Mats Zuccarello", "Chris Kreider", "Chris Kreider"
), primary_assist = c("Filip Chytil", "Mika Zibanejad", "Adam McQuaid", 
                      "Kevin Hayes", "Brett Howden", "Brady Skjei", "Mats Zuccarello", 
                      "Anthony DeAngelo", "Mats Zuccarello", "Pavel Buchnevich", NA, 
                      "Marc Staal", "Kevin Shattenkirk", "Mats Zuccarello", "Chris Kreider", 
                      "Neal Pionk", "Neal Pionk", "Neal Pionk", "Adam McQuaid", "Neal Pionk", 
                      "Neal Pionk", "Mika Zibanejad", "Jesper Fast", "Brendan Smith", 
                      "Pavel Buchnevich", "Vladislav Namestnikov", "Anthony DeAngelo", 
                      "Kevin Shattenkirk", "Brady Skjei", "Mats Zuccarello")), row.names = c(NA, 
                                                                                             -30L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("goal", 
                                                                                                                                                         "primary_assist"))
library(tidyverse)
mydata %>%
  # tidy the data
  gather(type, players) %>%
  # count the player
  count(players)
#> # A tibble: 19 x 2
#>    players                   n
#>    <chr>                 <int>
#>  1 Adam McQuaid              2
#>  2 Anthony DeAngelo          3
#>  3 Brady Skjei               3
#>  4 Brendan Smith             2
#>  5 Brett Howden              3
#>  6 Chris Kreider             7
#>  7 Filip Chytil              1
#>  8 Jesper Fast               2
#>  9 Jimmy Vesey               3
#> 10 Kevin Hayes               3
#> 11 Kevin Shattenkirk         2
#> 12 Marc Staal                1
#> 13 Mats Zuccarello           7
#> 14 Mika Zibanejad            7
#> 15 Neal Pionk                5
#> 16 Pavel Buchnevich          5
#> 17 Ryan Spooner              1
#> 18 Vladislav Namestnikov     2
#> 19 <NA>                      1

Created on 2019-01-15 by the reprex package (v0.2.1)

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