hadley / r4ds

R for data science: a book
http://r4ds.hadley.nz
Other
4.51k stars 4.19k forks source link

"Figure 20.16" is wrong #1584

Closed yyzeng closed 10 months ago

yyzeng commented 11 months ago

https://r4ds.hadley.nz/joins#rolling-joins

"Figure 20.16: A rolling join is similar to a greater-than-or-equal inequality join but only matches the first value." is wrong!

library(tidyverse)

x <- tibble(id = 1:3, x = c("x1", "x2", "x3"))
y <- tibble(id = c(1,2,4), y = c("y1", "y2", "y4"))
x %>% left_join(y, by = join_by(id <= id))
#> # A tibble: 6 × 4
#>    id.x x      id.y y    
#>   <int> <chr> <dbl> <chr>
#> 1     1 x1        1 y1   
#> 2     1 x1        2 y2   
#> 3     1 x1        4 y4   
#> 4     2 x2        2 y2   
#> 5     2 x2        4 y4   
#> 6     3 x3        4 y4
x %>% left_join(y, by = join_by(closest(id <= id)))
#> # A tibble: 3 × 4
#>    id.x x      id.y y    
#>   <int> <chr> <dbl> <chr>
#> 1     1 x1        1 y1   
#> 2     2 x2        2 y2   
#> 3     3 x3        4 y4

Created on 2023-10-16 with reprex v2.0.2

mine-cetinkaya-rundel commented 10 months ago

Duplicate of #1470, so will track there. Thank you!