data.table, 'data.frame' 'data.table' (setDT(df1)), 'Competing_Country', 'Year', sum Total_Medals and then " .
library(data.table)
setDT(df1)[,list(SumOfMedals = sum(Total_Medals)),
by = .(Competing_Country, Year)
][order(-Competing_Country, -Year, -SumOfMedals)]
dplyr .
library(dplyr)
df1 %>%
group_by(Competing_Country, Year) %>%
summary(SumOfMedals = sum(Total_Medals) %>%
arrange(desc(Competing_Country), desc(Year), desc(SumOfMedals))
df1 <- structure(list(Athlete = c("Michael Phelps", "Alicia Coutts",
"Missy Franklin", "Brian Leetch", "Mario Lemieux", "Ylva Lindberg",
"Eric Lindros", "Ulrica Lindström", "Shelley Looney"), Competing_Country = c("United States",
"Australia", "United States", "United States", "Canada", "Sweden",
"Canada", "Sweden", "United States"), Year = c(2012L, 2012L,
2012L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L), Total_Medals = c(6L,
5L, 5L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("Athlete", "Competing_Country",
"Year", "Total_Medals"), class = "data.frame", row.names = c(NA,
-9L))