fuzzyjoin with dates in R

前端 未结 1 1255
天涯浪人
天涯浪人 2021-01-21 17:04

I am working on a project where I am analyzing individual-level survey data within countries based on outcomes of sports matches across countries and I am not sure what the most

相关标签:
1条回答
  • 2021-01-21 17:49

    There are three issues

    1. Replace the double quote with backquote inside the match_fun

    2. the by values should be reversed

    3. 'date' columns are changed to respective Date class


    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
    
    0 讨论(0)
提交回复
热议问题