I have a list of baseball players (playerID). I want to find out how many different teams they were on when they played in their All Star games. For example, aaronha01 in the input played for the same team in five All Star appearances, so his NumTms is 1. The output image shows how I would like the output to be formatted.
CODE I tried this code, but it did not work. I would prefer an answer that uses dplyr.
df %>% group_by(playerID) %>% select(playerID, teamID) %>% mutate(teams = n_distinct(playerID)) %>% arrange(desc(teams))
INPUT
structure(list(playerID = c("aaronha01", "aaronha01", "aaronha01",
"aaronha01", "aaronha01", "aasedo01", "abreubo01", "abreubo01",
"abreujo02", "abreujo02", "abreujo02", "acunaro01", "adamsac01",
"adcocjo01", "adcocjo01", "ageeto01", "ageeto01", "aguilje01",
"aguilri01", "aguilri01", "aguilri01", "aguirha01", "albieoz01",
"alcansa01", "alexado01", "alfoned01", "allendi01", "allendi01",
"allendi01", "allendi01", "allendi01", "allendi01", "allendi01"
), teamID = c("MLN", "MLN", "MLN", "MLN", "MLN", "BAL", "PHI",
"PHI", "CHA", "NYN", "ATL", "ATL", "NY1", "MLN", "DET", "CHA",
"CHA", "MIL", "MIN", "MIN", "MIN", "DET", "ATL", "MIA", "DET",
"NYN", "PHI", "PHI", "PHI", "SLN", "CHA", "CHA", "CHA"), lgID = c("NL",
"NL", "NL", "NL", "NL", "AL", "NL", "NL", "AL", "AL", "AL", "NL",
"NL", "NL", "NL", "AL", "AL", "NL", "AL", "AL", "AL", "AL", "NL",
"NL", "AL", "NL", "NL", "NL", "NL", "NL", "AL", "AL", "AL")), row.names = c(NA,
-33L), class = c("tbl_df", "tbl", "data.frame"))
How I would like the output to display:
I have tried to do it based on the stackoverflow post at How to add count of unique values by group to R data.frame, but was unsuccessful.