Jul*_*ian 3 r left-join dplyr fuzzyjoin
我正在开展一个项目,根据不同国家/地区的体育比赛结果分析国家/地区内个人层面的调查数据,但我不确定产生我想要的合并的最有效方法是什么。
我正在处理两个单独的数据集。一种包含嵌套在国家/地区内的个人级数据。数据可能如下所示:
country <- c(rep("Country A", 4), rep("Country B", 6))
date <- c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", rep("2000-01-01", 2), "2000-01-02", rep("2000-01-03", 3))
outcome <- rnorm(10)
individual_data <- cbind.data.frame(country, date, outcome)
rm(country, date, outcome)
Run Code Online (Sandbox Code Playgroud)
另一个有国家比赛级别的数据,看起来像这样:
date <- rep("2000-01-02", 2)
country <- c("Country A", "Country B")
opponent <- c("Country B", "Country A")
match_outcome <- c("L", "W")
match_data <- cbind.data.frame(date, country, opponent, match_outcome)
rm(date, country, opponent, match_outcome)
Run Code Online (Sandbox Code Playgroud)
在这个例子中,只有一场比赛是在 2000 年 1 月 2 日进行的,其中 A 国输给了 B 国。我想执行一个fuzzy_join与left_join这里相反的match_data比赛,individual_data即使日期不是精确的。
# incorrect
merged <- left_join(individual_data, match_data)
Run Code Online (Sandbox Code Playgroud)
我想在 3 天的范围内执行此操作,并且我希望在此范围内显示比赛前后的天数。最终产品看起来像这样:
country <- c(rep("Country A", 4), rep("Country B", 6))
date <- c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", rep("2000-01-01", 2), "2000-01-02", rep("2000-01-03", 3))
outcome <- rnorm(10)
opponent <- c(rep("Country B", 4), rep("Country A", 6))
match_outcome <- c(rep("L", 4), rep("W", 6))
match_date <- rep("2000-01-02", 10)
difference <- c(-1, 0, 1, 2, -1, -1, 0, rep(1, 3))
desired_output <- cbind.data.frame(country, date, outcome, opponent, match_outcome, match_date, difference)
rm(country, date, outcome, opponent, match_outcome, match_date, difference)
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗?我一直在为如何完成这件事而苦苦挣扎。这是我迄今为止尝试过的:
match_data$match_date_minus3 <- ymd(match_data$date) - days(3)
match_data$match_date_plus3 <- ymd(match_data$date) + days(3)
test_output <- fuzzy_left_join(individual_data, match_data,
by = c("country" = "country",
"match_date_minus3" = "date",
"match_date_plus3" = "date"),
match_fun = list("==", ">", "<"))
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误: Error in which(m) : argument to 'which' is not logical
作为参考,如果有人知道,我正在尝试复制Depeteris-Chauvin 等人的结果。2018 年。
有三个问题
用反引号替换双引号 match_fun
该by数值应被逆转
“日期”列更改为相应的Date类
library(fuzzyjoin)
library(dplyr)
individual_data$date <- as.Date(individual_data$date)
match_data$match_date_minus3 <- as.Date(match_data$match_date_minus3)
match_data$match_date_plus3 <- as.Date(match_data$match_date_plus3)
fuzzy_left_join(individual_data, match_data,
by = c("country" = "country",
'date' = "match_date_minus3",
'date' = "match_date_plus3"),
match_fun = list(`==`, `>`, `<`)) %>%
select(country = country.x, date = date.x, outcome,
opponent, match_outcome, match_date = date.y)
# country date outcome opponent match_outcome match_date
#1 Country A 2000-01-01 1.4003662 Country B L 2000-01-02
#2 Country A 2000-01-02 0.5526607 Country B L 2000-01-02
#3 Country A 2000-01-03 0.4316405 Country B L 2000-01-02
#4 Country A 2000-01-04 -0.1171910 Country B L 2000-01-02
#5 Country B 2000-01-01 1.3433921 Country A W 2000-01-02
#6 Country B 2000-01-01 -1.1773011 Country A W 2000-01-02
#7 Country B 2000-01-02 -0.6953120 Country A W 2000-01-02
#8 Country B 2000-01-03 1.3484053 Country A W 2000-01-02
#9 Country B 2000-01-03 -0.7266405 Country A W 2000-01-02
#10 Country B 2000-01-03 -0.9139988 Country A W 2000-01-02
Run Code Online (Sandbox Code Playgroud)