Matrix from the Baseball dataset

Hello everybody,

I need to make a matrix from the Baseball dataset ( contains the ID of players who played on a certain team in a certain year). New matrix should indicate how many players played on several teams.
For example, teams ATL and BAL and we write in this box all the players who played on both the first and second teams. In this way we get a symmetric matrix.

Thanks a lot for your help

Try this:


df <- tibble(
  year = c(2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021),
  id = c("A", "B", "B", "D", "A", "B","D","D"),
  team = c("T1", "T2", "T3", "T4", "T3", "T2", "T1", "T4")

So in 2020 Mr. B played in T2 and T3 teams. In 2021 Mr. D played in T1 and T4 teams

y <- unique(df$year)

for(i in y){
a <- df %>%
  filter(year == i) %>% 
  group_by(id) %>%
  mutate(n = n()) %>% 
  filter(n > 1) %>% 


Thanks for your answer!

But I still don't understand how I can go through all the teams in pairs from the dataset to record the number of players for each pair of teams
Like this:


Can you paste part of your dataset using dput(head(yourdataframe, 20))?

This topic was automatically closed 42 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.