Home > Mobile >  Dplyr non-equi join no longer accepting join_by argument
Dplyr non-equi join no longer accepting join_by argument

Time:10-21

This code worked a couple days ago:

df<- tribble(
  ~ unique_id, ~event_type, ~ event_date,
  'id_101', 'A_type_event', '2022-01-01',
  'id_101', 'B_type_event', '2022-02-01',
  'id_101', 'A_type_event', '2022-02-15',
  'id_101', 'A_type_event', '2022-02-28',
  'id_101', 'B_type_event', '2022-03-01',
  'id_101', 'C_type_event', '2022-03-10',
  'id_101', 'A_type_event', '2022-03-20',
  'id_101', 'C_type_event', '2022-04-01'
)  
 left_join(
  df %>% filter(event_type == "A_type_event"),  # match A_type_event
  df %>% filter(event_type == "C_type_event"),   # with C_type_event
  #join_by(event_date < event_date),          # where A_type_event before C_type_event
  join_by(unique_id, event_date < event_date), # ... and unique id matches
  multiple = "first")                        # and just keep first match

Error code now is: Error in dplyr::common_by(): ! by must be a (named) character vector, list, or NULL for natural joins (not recommended in production code), not a <dplyr_join_by> object.

I tested the sample code on rdrr.io and another related stack overflow question on the same topic and they both return the same error.

Any idea how to fix the code?

CodePudding user response:

It is not clear about the version used in the question. join_by is in devel version of dplyr. The code works if we install the devel version as

devtools::install_github("tidyverse/dplyr")

library(dplyr)
> left_join(
    df %>% filter(event_type == "A_type_event"),  # match A_type_event
    df %>% filter(event_type == "C_type_event"),   # with C_type_event
    #join_by(event_date < event_date),          # where A_type_event before C_type_event
    join_by(unique_id, event_date < event_date), # ... and unique id matches
    multiple = "first")                       
# A tibble: 4 × 5
  unique_id event_type.x event_date.x event_type.y event_date.y
  <chr>     <chr>        <chr>        <chr>        <chr>       
1 id_101    A_type_event 2022-01-01   C_type_event 2022-03-10  
2 id_101    A_type_event 2022-02-15   C_type_event 2022-03-10  
3 id_101    A_type_event 2022-02-28   C_type_event 2022-03-10  
4 id_101    A_type_event 2022-03-20   C_type_event 2022-04-01  
  • Related