SebKrantz / collapse

Advanced and Fast Data Transformation in R
https://sebkrantz.github.io/collapse/
Other
651 stars 34 forks source link

Full join silently becomes a left join if there are no matches in key #574

Closed D3SL closed 4 months ago

D3SL commented 5 months ago

Attempting to join two dataframes when there are no matching rows in the join key results in silently failing to a left join. I think an ultra simple solution here would be a quick check at the start of the join function and if there's no matches found default to performing rbindlist(use.names=TRUE, fill=TRUE, list(x,y))

library(collapse)

# This will silently fail to a left join, returning all columns from X and Y but only rows from X
join(
  data.frame(key=1:5, lower=letters[1:5]),
 data.frame(key=6:10,upper=LETTERS[6:10]),
  how="full",
  on="key"
)

full join: x[key] 0/5 (0%) <m:m> y[key] 0/5 (0%)
  key lower upper
1   1     a  <NA>
2   2     b  <NA>
3   3     c  <NA>
4   4     d  <NA>
5   5     e  <NA>

# this will return rows from X and Y as expected
join(
  data.frame(key=1:5, lower=letters[1:5]),
 data.frame(key=5:10,upper=LETTERS[5:10]),
  how="full",
  on="key"
)
full join: x[key] 1/5 (20%) <m:m> y[key] 1/6 (16.7%)
   key lower upper
1    1     a  <NA>
2    2     b  <NA>
3    3     c  <NA>
4    4     d  <NA>
5    5     e     E
6    6  <NA>     F
7    7  <NA>     G
8    8  <NA>     H
9    9  <NA>     I
10  10  <NA>     J
SebKrantz commented 5 months ago

Thanks, will take care of this soon.