2
$\begingroup$

I would like to JoinAcross two datasets and use the KeyCollisionFunction. Basically, I would like to keep all rows from the left dataset (hence "Left"), add columns from the right dataset, but if some are already present in the left dataset then I want to use updated values from the right dataset (hence KeyCollisionFunction->Right). Example:

JoinAcross[
 Dataset[{<|"a" -> 1, "b" -> 1, "c" -> Missing["Reason"]|>, <|
    "a" -> 2, "b" -> 8, "c" -> Missing["Reason"]|>, <|"a" -> 2, 
    "b" -> 2, "c" -> 5|>}],
 Dataset[{<|"a" -> 1, "c" -> 1|>, <|"a" -> 2, "c" -> 6|>}],
 "a",
 "Left",
 KeyCollisionFunction -> Right
 ]

The code above fails with error JoinAcross::invlc: The argument Dataset [<<3>>] is not a list of Associations. It works without the KeyCollisionFunction, but returns column C with values from the left dataset.

The only solution I found so far is conversion to Associations and then back to a Dataset, but that seems to be quite cumbersome and I am not sure whether it can cause any problems:

Dataset@JoinAcross[
  Normal@Dataset[{<|"a" -> 1, "b" -> 1, "c" -> Missing["Reason"]|>, <|
      "a" -> 2, "b" -> 8, "c" -> Missing["Reason"]|>, <|"a" -> 2, 
      "b" -> 2, "c" -> 5|>}],
  Normal@Dataset[{<|"a" -> 1, "c" -> 1|>, <|"a" -> 2, "c" -> 6|>}],
  "a",
  "Left",
  KeyCollisionFunction -> Right
  ]
$\endgroup$
1
$\begingroup$

Another solution I came up with is to switch the two datasets, use "Right" and omit the KeyCollisionFunction altogether. It changes the order of columns and it relies on the default KeyCollisionFunction settings (Left), but produces the correct result:

JoinAcross[
 Dataset[{<|"a" -> 1, "c" -> 1|>, <|"a" -> 2, "c" -> 6|>}],
 Dataset[{<|"a" -> 1, "b" -> 1, "c" -> Missing["Reason"]|>, <|
    "a" -> 2, "b" -> 8, "c" -> Missing["Reason"]|>, <|"a" -> 2, 
    "b" -> 2, "c" -> 5|>}],
 "a",
 "Right"
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.