简体   繁体   中英

R: Extract data from previous observation of same factor level in dataframe

I'm trying to create columns in a dataframe that should show the form of a given football team. Namely whether the respective football team has won, tied or lost its previous game.

Think of the dataframe below as a sequence of football games. In the first game Liverpool defeats Chelsea. What I'm trying to do is to use this result to show Liverpool's form in its next game. So, when Liverpool then plays against Leicester, I want HomeForm to say "W" (for won) and AwayForm to also say "W" since Leicester also won it's previous game.

The problem , in a sentence, is that I don't know any function to tell R to identify the previous one (or two) observation(s) of one particular factor level (Liverpool for example), and then extract the data I want from that one (or two) previous observation(s).

HomeTeam <- c("Liverpool", "ManCity", "ManUnited", "Tottenham", "Arsenal", "Leicester", "Chelsea", "Leeds", "Liverpool", "ManCity", "ManUnited", "Tottenham", "Arsenal", "Leicester", "Chelsea","Liverpool", "ManCity", "ManUnited", "Tottenham", "Arsenal", "Leicester", "Chelsea", "Leeds", "Liverpool", "ManCity", "ManUnited", "Tottenham", "Arsenal", "Leicester", "Chelsea")
AwayTeam <- c("Chelsea", "Tottenham", "Arsenal", "ManUnited", "Leicester", "ManCity", "Leeds", "Chelsea", "Leicester", "Liverpool", "Arsenal", "ManUnited", "Tottenham", "ManCity", "Liverpool","Chelsea", "Tottenham", "Arsenal", "ManUnited", "Leicester", "ManCity", "Leeds", "Chelsea", "Leicester", "Liverpool", "Arsenal", "ManUnited", "Tottenham", "ManCity", "Liverpool")
Result <- c("H","H","D","A","H","A","H","D","H","A","A","H","D","A","A")

data <- data.frame(HomeTeam,AwayTeam,Result)

data$HomeForm <- 0
data$AwayForm <- 0

Okay so this code 1) takes your dataframe 2) changes it to long format 3) lags the result column and thereby gives you the previousform and 4) returns to wide form. Hope this helps.

teams <- c("Liver", "Man", "United", "Totten")
Result <- c("Home","Draw", "Away")
data <- data.frame("Home" = sample(teams, 100, replace = T),
                   "Away" = sample(teams, 100, replace = T),
                   "Result" = sample(Result, 100, replace = T))
        
data <- data[-which(data$Home == data$Away),]


data %>%
mutate("indexofgames" = 1:n()) %>%
pivot_longer(., c("Home","Away")) %>%
mutate("Result" = ifelse(Result == name, "Win", ifelse(Result == "Draw", "Draw", "Loss"))) %>%
group_by(value) %>%
arrange(indexofgames) %>%
mutate("previousform" = lag(Result)) %>%
pivot_wider(.,id_cols = indexofgames,names_from = c("name"),values_from = c("value", "previousform","Result"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM