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)
individual_data <- cbind.data.frame(country, date, outcome)
rm(country, date, outcome)

另一个有国家匹配级别的数据,看起来像这样:

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)

在这个例子中,只有一场比赛,在 2000 年 1 月 2 日进行,A 国输给了 B 国。我想执行 afuzzy_join以便与这里left_join相反,即使日期不是精确的。match_dataindividual_data

# incorrect
merged <- left_join(individual_data, match_data)

我想在 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)

谁能帮我吗?我一直在为如何完成这项工作而苦苦挣扎。这是我迄今为止尝试过的:

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("==", ">", "<"))

但我收到以下错误:Error in which(m) : argument to 'which' is not logical

如果有人知道,作为参考,我正在尝试复制Depeteris-Chauvin 等人的结果。2018 年

4

1 回答 1

6

存在三个问题

  1. 用反引号替换双引号match_fun

  2. by值应该颠倒

  3. “日期”列更改为相应的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
于 2019-11-05T19:54:43.193 回答