R:从先前对数据框中相同因子水平的观察中提取数据
R: Extract data from previous observation of same factor level in dataframe
我正在尝试在数据框中创建列,该列应显示给定足球队的形式。即各自的足球队在上一场比赛中是赢了、平了还是输了。
将下面的数据框想象成一系列足球比赛。在第一场比赛中,利物浦击败了切尔西。我想做的是用这个结果来展示利物浦在下一场比赛中的状态。
因此,当利物浦随后对阵莱斯特时,我希望 HomeForm 说“W”(表示获胜),而 AwayForm 也说“W”,因为莱斯特也赢得了上一场比赛。
问题,一句话,是我不知道任何函数来告诉 R 识别一个特定的前一个(或两个)观察因子水平(例如利物浦),然后从之前的一个(或两个)观察中提取我想要的数据。
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
好的,所以此代码 1) 获取您的数据框 2) 将其更改为长格式 3) 滞后于结果列,从而为您提供以前的格式和 4) returns 到宽格式。希望这有帮助。
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"))
我正在尝试在数据框中创建列,该列应显示给定足球队的形式。即各自的足球队在上一场比赛中是赢了、平了还是输了。
将下面的数据框想象成一系列足球比赛。在第一场比赛中,利物浦击败了切尔西。我想做的是用这个结果来展示利物浦在下一场比赛中的状态。 因此,当利物浦随后对阵莱斯特时,我希望 HomeForm 说“W”(表示获胜),而 AwayForm 也说“W”,因为莱斯特也赢得了上一场比赛。
问题,一句话,是我不知道任何函数来告诉 R 识别一个特定的前一个(或两个)观察因子水平(例如利物浦),然后从之前的一个(或两个)观察中提取我想要的数据。
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
好的,所以此代码 1) 获取您的数据框 2) 将其更改为长格式 3) 滞后于结果列,从而为您提供以前的格式和 4) returns 到宽格式。希望这有帮助。
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"))