I am just starting looking at network analysis and wanted to begin by creating a data.frame of how often basketball players on a team have started together
Ideally, I would like to incorporate map functions from purrr
So with this as input
game_1 <- c("Andy","Bob","Chris","Doug","Evan")
game_2 <- c("Andy","Chris","Evan","Fred","George")
I would want a result like this
n_1 n_2 games
Andy Bob 1
Andy Chris 2
Andy Doug 1
Andy Evan 2
Andy Fred 1
Andy George 1
Bob Chris 1
Bob Doug 1
Bob Evan 1
Chris Doug 1
Chris Evan 2
Chris Fred 1
Chris George 1
Doug Evan 1
Evan Fred 1
Evan George 1
Fred George 1
My solution does not use purrr
, but it should work
game_1 <- c("Andy","Bob","Chris","Doug","Evan")
game_2 <- c("Andy","Chris","Evan","Fred","George")
# Combine all games into a single list for use with lapply
all_games <- list(game_1, game_2)
library(dplyr)
# Find combinations, sorted to ensure the earlier alphabets are in the first column
df <- do.call(rbind, lapply(all_games, function(x) { data.frame(t(combn(sort(x), 2))) }))
# Calculate the number of instances where 2 players appear with each other
df %>% group_by(X1, X2) %>% summarise(count = n())
# A tibble: 17 x 3
# Groups: X1 [?]
# X1 X2 count
# <fctr> <fctr> <int>
# 1 Andy Bob 1
# 2 Andy Chris 2
# 3 Andy Doug 1
# 4 Andy Evan 2
# 5 Andy Fred 1
# 6 Andy George 1
# 7 Bob Chris 1
# 8 Bob Doug 1
# 9 Bob Evan 1
# 10 Chris Doug 1
# 11 Chris Evan 2
# 12 Chris Fred 1
# 13 Chris George 1
# 14 Doug Evan 1
# 15 Evan Fred 1
# 16 Evan George 1
# 17 Fred George 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With