# JoinAcross datasets based on Equal and not on SameQ

JoinAcross will join keys if values are the same (SameQ==True) but not if there are equal. For example these two associations are not joined :

JoinAcross[{<|a -> 1, b -> X|>}, {<|a -> 1., c -> Y|>}, Key[a]]

Does it exist a way to simply join across datasets based on equal values with keeping the left or right value?

• Maybe Merge[{<|a -> 1, b -> X|>, <|a -> 1., c -> Y|>}, First] or Merge[{<|a -> 1, b -> X|>, <|a -> 1., c -> Y|>}, Last]? – Henrik Schumacher Apr 19 '18 at 9:16
• Unfortunately it does not work. Try an answer for JoinAcross[{<|a -> 1, b -> X|>, <|a -> 2, b -> XX|>, <|a -> 3, b -> XXX|>}, {<|a -> 1, c -> Y|>, <|a -> 2, c -> YY|>, <|a -> 3, c -> YYY|>}, Key[a]] with replacing for example '1' with '1.' – Porty Apr 19 '18 at 10:07
• Or by just applying N onto the input? data = { {<|a -> 1, b -> X|>, <|a -> 2, b -> XX|>, <|a -> 3, b -> XXX|>}, {<| a -> 1., c -> Y|>, <|a -> 2, c -> YY|>, <|a -> 3, c -> YYY|>} }; JoinAcross[Sequence @@ N@data, Key[a]] – Henrik Schumacher Apr 19 '18 at 10:16
• Some of my values are numerical values, other are dateobjects not in the same timezone so I am looking for a general method to JoinAcross based on equal values and not same values. – Porty Apr 19 '18 at 10:17
• Hm. Many set related functions have an option SameTest, but JoinAcross does not, unfortunately. – Henrik Schumacher Apr 19 '18 at 10:20

I haven't tested this function very thoroughly yet, but I think this should get you started. The idea is to go through one of the lists and replace the elements on the joining key with elements "projected" on the elements from the other list (under the specified sametest). After that, I think you should be able to use the regular JoinAcross:

joinAcross2[
list1 : {__?AssociationQ},
list2 : {__?AssociationQ},
sameTest_,
key : (_String | _Key),
jspec : ("Inner" | "Outer" | "Left" | "Right") : "Inner"
] := Module[{
vals1 = list1[[All, key]]
},
JoinAcross[
list1,
MapAt[
Function[
element,
SelectFirst[vals1, sameTest[element, #] &, element]
],
list2,
{All, key}
],
key,
jspec
]
];


As you can see, I've only implemented this for joining on a single key.

Test:

In[208]:= joinAcross2[{<|a -> 1, b -> X|>}, {<|a -> 1., c -> Y|>}, Equal, Key[a]]

Out[208]= {<|a -> 1, b -> X, c -> Y|>}


edit Performance:

If the computation time is prohibitive, you can try to rewrite the MapAt function inside if that's the limiting step. Right now, we map over the column of a list of associations. You could instead try to compute the column values first and use in-place assignment to modify the original dataset. So we'd replace the MapAt with something like:

Module[{dataset = list2},
dataset[[All, key]] = Map[
Function[element,
SelectFirst[vals1, sameTest[element, #] &, element]
],
dataset[[All, key]]
];
dataset
]


However, this will only be worthwhile if your sameTest can be compiled. If that's the case, the Map should be pretty fast. Try using Compile to see if you can get a compiled version of Function[element, ...].

• Thanks you very much for your help. It does the job. The only drawaback is the computation time which is very high for large datasets. – Porty Apr 20 '18 at 8:46
• I added a suggestion for improving the computation time. However, I would recommend you first do a sanity check to see what's the limiting step in your computation: the inner MapAt or the outer JoinAcross. – Sjoerd Smit Apr 20 '18 at 9:02
• Thanks again for your help. I have tested and this is the MapAt function which is very very slow. The issue is I have dateobjects with timezone inside the sameTest (equal test for my case where I wan't to joinacross some datasets with dateobjets not in the same timezone) so I don't know if it is possible to compile such a test with dateobjects? – Porty Apr 21 '18 at 11:43
• You certainly can't compile DateObjects, but you could try to rewrite your sameTest using AbsoluteTime. Often that speeds things up quite a bit. In the worst case scenario, you'll have to convert all date objects to numbers with AbsoluteTime and convert them back afterwards. You might also try using Dataset to see if that improves the speed of the mapping. – Sjoerd Smit Apr 21 '18 at 17:41