Home > OS >  Compute each unique value in a variable in R
Compute each unique value in a variable in R

Time:12-12

QUESTION that I have to solve: The winner of each match is the team who has the highest score. Each team scores 3 points for a win and 1 point for a draw and the team with the most points at the end of the season is the season winner. Write the code to compute each team’s points after the first 5 rounds of matches and the full season. Provide the results for a season in a table (two tables, one table for each set of results), ordering teams by their points (i.e. a results ladder).

So I need to compute each team Point by season. But there are 16 teams and I wanna find a smarter way rather than subset each team separately.

After calculation I will need to put each team score in an order

Here are the dataset that I already take out the season I wanna work on

This is my current solution but it is still not giving the correct answer

 season2015 <- df[df$Season == 2015,]
    season2015 %>% 
      distinct(homeTeam)   #to see how many teams there are

#Subset each team
    UnitedStatesChimeras <- season2015[season2015$homeTeam == "United States Chimeras",]
    Durhamstrang <- season2015[season2015$homeTeam == "Durhamstrang",]
    EnglandNightmares <- season2015[season2015$homeTeam == "England Nightmares",]
    ScotlandSquirrels <- season2015[season2015$homeTeam == "Scotland Squirrels",]
    WalesLongshots <- season2015[season2015$homeTeam == "Wales Longshots",]
    IrelandGriffins <- season2015[season2015$homeTeam == "Ireland Griffins",]
    FranceThestrals <- season2015[season2015$homeTeam == "France Thestrals",]
    GermanyWerewolves <- season2015[season2015$homeTeam == "Germany Werewolves",]
    TurkeyKnights <- season2015[season2015$homeTeam == "Turkey Knights",]
    PakistanSwans <- season2015[season2015$homeTeam == "Pakistan Swans",]
    MalaysiaTornadoes <- season2015[season2015$homeTeam == "Malaysia Tornadoes",]
    DenmarkHorntails <- season2015[season2015$homeTeam == "Denmark Horntails",]
    FranceHippogriffsFirsts <- season2015[season2015$homeTeam == "France Hippogriffs Firsts",]
    NewZealandDaemons <- season2015[season2015$homeTeam == "New Zealand Daemons",]
    AustraliaCenturions <- season2015[season2015$homeTeam == "Australia Centurions",]
    JapanFalcons <- season2015[season2015$homeTeam == "Japan Falcons",]

#Compute the point
    UnitedStatesChimeras <- sum(UnitedStatesChimeras$homePoint, UnitedStatesChimeras$awayPoint)
    Durhamstrang <- sum(Durhamstrang$homePoint, Durhamstrang$awayPoint)
    EnglandNightmares <- sum(EnglandNightmares$homePoint, EnglandNightmares$awayPoint)
    ScotlandSquirrels <- sum(ScotlandSquirrels$homePoint, ScotlandSquirrels$awayPoint)
    WalesLongshots <- sum(WalesLongshots$homePoint, WalesLongshots$awayPoint)
    IrelandGriffins <- sum(IrelandGriffins$homePoint, IrelandGriffins$awayPoint)
    FranceThestrals <- sum(FranceThestrals$homePoint, FranceThestrals$awayPoint)
    GermanyWerewolves <- sum(GermanyWerewolves$homePoint, GermanyWerewolves$awayPoint)
    TurkeyKnights <- sum(TurkeyKnights$homePoint, TurkeyKnights$awayPoint)
    PakistanSwans <- sum(PakistanSwans$homePoint, PakistanSwans$awayPoint)
    MalaysiaTornadoes <- sum(MalaysiaTornadoes$homePoint, MalaysiaTornadoes$awayPoint)
    DenmarkHorntails <- sum(DenmarkHorntails$homePoint, DenmarkHorntails$awayPoint)
    FranceHippogriffsFirsts <- sum(FranceHippogriffsFirsts$homePoint, FranceHippogriffsFirsts$awayPoint)
    NewZealandDaemons <- sum(NewZealandDaemons$homePoint, NewZealandDaemons$awayPoint)
    AustraliaCenturions <- sum(AustraliaCenturions$homePoint, AustraliaCenturions$awayPoint)
    JapanFalcons <- sum(JapanFalcons$homePoint, JapanFalcons$awayPoint)
    season2015_totalpoint <- c(UnitedStatesChimeras,
                               Durhamstrang,
                               EnglandNightmares,
                               ScotlandSquirrels,
                               WalesLongshots,
                               IrelandGriffins,
                               FranceThestrals,
                               GermanyWerewolves,
                               TurkeyKnights,
                               PakistanSwans,
                               MalaysiaTornadoes,
                               DenmarkHorntails,
                               FranceHippogriffsFirsts,
                               NewZealandDaemons,
                               AustraliaCenturions,
                               JapanFalcons
                               )
    lapply(season2015_totalpoint, sort,decreasing=TRUE) #put the point of each team in order

I need a smarter, better way to do this. My method is a mess

Please help!!!

CodePudding user response:

You say there are 16 teams, but there are 22 in the dataset? Anyways, here is a way to calculate the total points per season for each team.

library(tidyverse)

df <- read_csv("competitionResults.csv") %>% 
  janitor::clean_names()

# Repliace OPs dataset
df <- df %>% 
  mutate(snitch_catcher = case_when(snitch == home_team ~ "home", 
                                    TRUE ~ "away"), 
         home_score = home_goals * 10, 
         away_score = away_goals * 10, 
         home_point = case_when(home_score > away_score ~ 3, 
                                home_score == away_score ~ 1, 
                                TRUE ~ 0), 
         away_point = case_when(away_score > home_score ~ 3, 
                                away_score == home_score ~ 1,
                                TRUE ~ 0), 
         match_number = 1:nrow(.)) 

# Points as home_team per season
home <- df %>%  
  group_by(team = home_team, season) %>%  
  summarise(home_points = sum(home_point)) %>% 
  arrange(team, season)

# Points as away team per season
away <- df %>%  
  group_by(team = away_team, season) %>%  
  summarise(away_points = sum(away_point)) %>% 
  arrange(team, season) 

left_join(home, away) %>%  
  mutate(total_points = home_points   away_points)

# A tibble: 174 x 5
# Groups:   team [22]
   team                 season home_points away_points total_points
   <chr>                 <dbl>       <dbl>       <dbl>        <dbl>
 1 Australia Centurions   2012          21          22           43
 2 Australia Centurions   2013          26          13           39
 3 Australia Centurions   2014          18          10           28
 4 Australia Centurions   2015          24          22           46
 5 Australia Centurions   2016          28          28           56
 6 Australia Centurions   2017          24          23           47
 7 Australia Centurions   2018          34          24           58
 8 Australia Centurions   2019          37          29           66
 9 Australia Centurions   2020          32          37           69
10 Australia Centurions   2021          26          32           58
11 China Nifflers         2021          28          46           74
12 Denmark Horntails      2012          17          15           32
13 Denmark Horntails      2013          19          12           31
14 Denmark Horntails      2014          30          14           44
15 Denmark Horntails      2015          24          21           45
16 Denmark Horntails      2016          15          18           33
17 Denmark Horntails      2017          19          15           34
18 Denmark Horntails      2018          26          35           61
19 Denmark Horntails      2019          28          19           47
20 Denmark Horntails      2020          34          27           61
21 Denmark Horntails      2021          30          33           63
22 Durhamstrang           2012          21          24           45
23 Durhamstrang           2013          18          25           43
24 Durhamstrang           2014          31          26           57
25 Durhamstrang           2015          22          15           37
26 Durhamstrang           2016          28          27           55
27 Durhamstrang           2017          20          13           33
28 Durhamstrang           2018          36          27           63
29 Durhamstrang           2019          19          29           48
30 Durhamstrang           2020          27          26           53
# ... with 144 more rows
  • Related