t.wangd
t.wangd

Reputation: 13

R - DF frequency

I have a dataset of all the matches played by LA Lakers from 2005 to 2015. It looks like this:

Hometeam         Guest           Result     Value
LA Lakers       NY Knicks          H         1
Chicago Bulls   LA Lakers          G         1
LA Lakers      Houston Rockets     D         0
LA Lakers       NY Knicks          H         1
Chicago Bulls   LA Lakers          G         1
LA Lakers      Houston Rockets     D         0
LA Lakers      Walsh HS            H         1
LA Lakers      Ga Tech             G         0
Akron Beacon    LA Lakers          G         1
New Orleans     LA Lakers          H         0

Where

     H - Home team won
     G - Guest team won
     D - Draw

Now, I want to find the frequencies in order to find the probability- of LA Lakers winning if they won a previous match.

How can I find the frequency of LA Lakers winning in two consecutive matches both playing in home and playing as guest. I'm trying to solve this in R. Thanks in Advance.

Now, I want to calculate the frequency of LA Lakers (won/lost) this game won/lost previous game.

So the four ordered pairs are (won P, won C),(won P, Lost C),(Lost P, Won C), (Lost P, Lost C).

Upvotes: 1

Views: 65

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388982

Probably, you can do :

library(dplyr)

#Win and win
df %>%  summarise(total = sum(Value == 1 & lag(Value, default = 0) == 1))

#Win and lose
df %>% summarise(total = sum(Value == 0 & lag(Value, default = 0) == 1))

#Lose and Win
df %>% summarise(total = sum(Value == 1 & lag(Value, default = 1) == 0))

#Lose and Lose
df %>% summarise(total = sum(Value == 0 & lag(Value, default = 1) == 0))

Upvotes: 0

David T
David T

Reputation: 2143

df <- read_table("Hometeam         Guest           Result
LA Lakers       NY Knicks          H
Chicago Bulls   LA Lakers          G
LA Lakers      Houston Rockets     D
LA Lakers      Walsh HS            H
LA Lakers      Ga Tech             H
Akron Beacon    LA Lakers          G
New Orleans     LA Lakers          H")

ScoreCard <- df %>% 
  mutate(LAKERS.WON = case_when(
    Hometeam == "LA Lakers" & Result == "H" ~ TRUE,
    Guest == "LA Lakers" & Result == "G" ~ TRUE,
    TRUE ~ FALSE),
    LAK.WIN.NEXT = lead(LAKERS.WON),
    LAK.WIN.SUCCESIVE = LAKERS.WON & LAK.WIN.NEXT)

 Hometeam      Guest           Result LAKERS.WON LAK.WIN.NEXT LAK.WIN.SUCCESIVE
  <chr>         <chr>           <chr>  <lgl>      <lgl>        <lgl>            
1 LA Lakers     NY Knicks       H      TRUE       TRUE         TRUE             
2 Chicago Bulls LA Lakers       G      TRUE       FALSE        FALSE            
3 LA Lakers     Houston Rockets D      FALSE      TRUE         FALSE            
4 LA Lakers     Walsh HS        H      TRUE       TRUE         TRUE             
5 LA Lakers     Ga Tech         H      TRUE       TRUE         TRUE             
6 Akron Beacon  LA Lakers       G      TRUE       FALSE        FALSE            
7 New Orleans   LA Lakers       H      FALSE      NA           FALSE        


Win1 <- ScoreCard %>% 
  filter(LAKERS.WON)

with(Win1, sum(LAK.WIN.SUCCESIVE) / sum(LAKERS.WON))  
[1] 0.6

Upvotes: 1

Related Questions