if (require("Lahman")) {
batting_tbl <- tbl_df(Batting)
tally(group_by(batting_tbl, yearID))
tally(group_by(batting_tbl, yearID), sort = TRUE)
# Multiple tallys progressively roll up the groups
plays_by_year <- tally(group_by(batting_tbl, playerID, stint), sort = TRUE)
tally(plays_by_year, sort = TRUE)
tally(tally(plays_by_year))
# This looks a little nicer if you use the infix %>% operator
batting_tbl %>% group_by(playerID) %>% tally(sort = TRUE)
# count is even more succinct - it also does the grouping for you
batting_tbl %>% count(playerID)
batting_tbl %>% count(playerID, wt = G)
batting_tbl %>% count(playerID, wt = G, sort = TRUE)
}
Run the code above in your browser using DataLab