2017-12-06 23 views
0

私はこのようなユニークなマッチのデータセットを持っています。各行は結果と一致します。私は2つの新しい列を作成したいRの同じデータセット内の一致する行

date <- c('2017/12/01','2017/11/01','2017/10/01','2017/09/01','2017/08/01','2017/07/01','2017/06/01') 
team1 <- c('A','B','B','C','D','A','B') 
team1_score <- c(1,0,4,3,5,6,7) 
team2 <- c('B','A','A','B','C','C','A') 
team2_score <- c(0,1,5,4,6,9,10) 
matches <- data.frame(date, team1, team1_score, team2, team2_score) 

、試合の結果2チーム1とチームのための形式はどのチームによって決定することができ、より大きなスコアやドローを持っています。結果は以下のようになります。したがって、フォームは最後の2試合でteam1の結果となります。たとえば、最初の3行では、チーム1とチーム2の形式がそれぞれあります。特定のチームの2つの試合が十分でない場合がありますので、NULLの結果で十分です。チーム1とチーム2の形式が一致することを知りたい。

  • をForm1:W-W、L-W、W-L
  • のForm2:L-L、W-L、L-W

実際のデータセットでは、わずか4ユニークなチームよりも多くがあります。私は思っていましたが、これらの2つの変数を作成する良い方法を考えることはできません。ここで

+2

どのようにして、結果を知っていますか? 「B」のスコアは1のスコアを上回っていますか? – Gregor

+1

再現可能な例を示してください。再現可能なデータセットを提供するのに役立つ関数dput()を探します。 多くのオプションがあります。データが長い(すなわち整然とした)形式である場合、W | Lのような形式で、または例3、2に従って、結果を得る勝ち負けのローリング・サムを持つことができます。 if_else関数を使用して、文字列形式の計算結果を最後の5回の反復に対して作成し、それらの文字列を連結することもできます。 –

+0

@ Gregor:そうです、チームはTeam1またはTeam2に入ることができます。もしスコアが大きければ、勝つことになります。 –

答えて

0

は私のソリューションです:

library(tidyverse) 


    date <- as.Date(c('2017/12/01','2017/11/01','2017/10/01','2017/09/01','2017/08/01','2017/07/01','2017/06/01', '2017/05/30')) 
    team1 <- c('A','B','B','C','D','A','B','A') 
    team1_score <- c(1,0,4,3,5,6,7,0) 
    team2 <- c('B','A','A','B','C','C','A','D') 
    team2_score <- c(0,1,5,4,6,9,10,0) 
    matches <- data.frame(date, team1, team1_score, team2, team2_score) 

    ## 1. Create a unique identifier for each match. It assumes that teams can only play each other once a day. 
    matches$UID <- paste(matches$date, matches$team1, matches$team2, sep = "-") 

    ## 2. Create a Score Difference Varaible reflecting team1's score 
    matches <- matches %>% mutate(score_dif_team1 = team1_score - team2_score) 

    ## 3. Create a Result (WDL) reflecting team1's results 
    matches <- matches %>% mutate(results_team1 = if_else(score_dif_team1 < 0, true = "L", false = if_else(score_dif_team1 > 0, true = "W", false = "D"))) 

    ## 4. Cosmetic step: Reorder variables for easier comparison across variables 
    matches <- matches %>% select(UID, date:results_team1) 

    ## 5. Reshape the table into a long format based on the teams. Each observation will now reflect the results of 1 team within a match. Each game will have two observations. 
    matches <- matches %>% gather(key = old_team_var, value = team, team1, team2) 

    ## 6. Stablishes a common results variable for each observation. It essentially inverts the results_team1 varaible for teams2, and keeps results_team1 identical for teams1 
    matches <- matches %>% 
       mutate(results = if_else(old_team_var == "team2", 
                true = if_else(results_team1 == "W", 
                    true = "L", 
                    false = if_else(results_team1 == "L", 
                        true = "W", 
                        false = "D")), 
                false = results_team1)) 

## Final step: Filter the matches table by the dates you are interested into, and then reshapes the table to show a data frame of DLW in long format. 

    Results_table <- matches %>% filter(date <= as.Date("2017-12-01")) %>% group_by(team, results) %>% summarise(cases = n()) %>% spread(key = results, value = cases, fill = 0) 

## Results: 
    # A tibble: 4 x 4 
    # Groups: team [4] 
     team  D  L  W 
    * <chr> <dbl> <dbl> <dbl> 
    1  A  1  1  4 
    2  B  0  4  1 
    3  C  0  1  2 
    4  D  1  1  0 
関連する問題