您好我有2个数据框,其中包含3个类似的列,访问者和日期
我想在这些条件下从dataframe italy中提取与newChamps匹配的行:
newItaly$home == newChamps$home | newItaly$visitor == newChamps$visitor & newItaly$Date >newChamps$Date
newItaly和newChamps没有相同数量的行。
更新
我仍然无法正确获得结果。这是代码:
install_github('jalapic/engsoccerdata', username = "jalapic")
LoadLibraries <- function(){
library(stringr)
library(plyr)
library(devtools)
library(engsoccerdata)
}
ChampsData <- function(){
filteredChamps <- champs[champs$hcountry == "ITA" | champs$vcountry == "ITA", ]
finalChamps <- subset(filteredChamps, select = -c(round, leg, FT, HT, aet, pens, FTagg_home, FTagg_visitor, aethgoal, aetvgoal, tothgoal, totvgoal, totagg_home, totagg_visitor, tiewinner) )
finalChamps$Date <- as.Date(finalChamps$Date, "%y/%m/%d")
finalChamps[,"Results"] <- NA
finalChamps$Results[finalChamps$hcountry == 'ITA' & finalChamps$hgoal > finalChamps$vgoal] <- "WIN"
finalChamps$Results[finalChamps$hcountry == 'ITA' & finalChamps$hgoal < finalChamps$vgoal] <- "LOSS"
finalChamps$Results[finalChamps$vcountry == 'ITA' & finalChamps$vgoal > finalChamps$hgoal] <- "WIN"
finalChamps$Results[finalChamps$vcountry == 'ITA' & finalChamps$vgoal < finalChamps$hgoal] <- "LOSS"
finalChamps$Results[finalChamps$vgoal == finalChamps$hgoal] <- "DRAW"
finalChamps<- finalChamps[order(finalChamps$Date),]
return(finalChamps)
}
ItalyData <- function(){
amendedItaly<- subset(italy, italy$Season>1954 & italy$Season<2016)
amendedItaly<- amendedItaly[order(amendedItaly$Date),]
amendedItaly$Date <- as.Date(amendedItaly$Date, "%y/%m/%d")
finalItaly <- subset(amendedItaly, select = -c(FT, tier) )
finalItaly[,"Results"] <- NA
finalItaly$Results <- ifelse(finalItaly$hgoal < finalItaly$vgoal, finalItaly$visitor, finalItaly$home)
finalItaly$Results[finalItaly$hgoal == finalItaly$vgoal] <- "DRAW"
return(finalItaly)
}
LoadLibraries()
newChamps <- ChampsData()
newItaly <- ItalyData()
t<- newItaly[which(newItaly$home %in% unique(newChamps$home) | newItaly$visitor %in% unique(newChamps$visitor) & newItaly$Date > newChamps$Date),]
基本上我正在努力匹配参加冠军联赛的球队以及在意大利联赛中参加过周中和周末比赛的球队。例如:如果米兰参加2/5/2018(欧洲冠军联赛),米兰参加6/5/2018(意大利联赛)
答案 0 :(得分:2)
我认为您正在寻找这样的事情:
COM
修改强>
newItaly[which(newItaly$home %in% unique(newChamps$home) |
newItaly$visitor %in% unique(newChamps$visitor) &
newItaly$Date > max(newChamps$Date) ),]
是可选的,您可以直接执行:
which