DavisVaughan · GitHub

We talked about making na_if() work in a match-like way, like na_if(x, c("bad", "NA")), but I no longer think we should do that. While that might be more useful, I think:

  • It would break existing code where people are currently supplying vector y values and expect == to be used, which does seem to come up on a quick GitHub scan

  • It would no longer match NULLIF() from SQL, which it is advertised as mimicking. The Microsoft SQL docs state that NULLIF(e1, e2) should be treated as a "searched CASE expression", meaning that the following should be exactly equivalent

    NULLIF(e1, e2)
    CASE
      WHEN e1 = e2 THEN NULL
      ELSE e1
    END
    

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql?view=sql-server-ver16#remarks

If people want a match-like na_if(), we could add replace_match(x, c("bad", "NA") ~ NA) as part of #6328


Other notes on what changes from this update:

  • Now casts y to the type of x, with the intention of making it very clear that this is type stable on x
  • Now uses vec_equal() for comparison
  • Now uses vec_assign() for assignment

"9c7db457-e0e3-4d62-8d92-1a8af03dae11"

Read the original on github.com ↗