Database like "left join" or SAS like Merge function

Discussion in 'Mathematica' started by Peter, Nov 23, 2007.

  1. Peter

    Peter Guest

    Is there a builtin function that can "join" (merge) two two-
    dimensional data lists based on a single or multiple key column?

    This seems like such a basic requirement that I feel like I must be
    missing something obvious.

    Thanks, Peter
    Peter, Nov 23, 2007
    1. Advertisements

  2. Here is an example that does what you want:

    Define a matrix where the first two columns contain keys and the third
    column contains data.
    In[1]:= mat1=Table[{RandomInteger[3],RandomInteger[2],RandomReal[]},{10}]

    Similarly, define a second matrix.
    In[2]:= mat2=Table[{RandomInteger[3],RandomInteger[2],RandomReal[]},{20}]

    Join the matrices, then sort the result by the key columns, then split the
    sorted result into subsets according to their key.
    In[3]:= Split[Sort[Join[mat1,mat2]],#1[[2]]==#2[[2]]&]

    This is a very basic example, but it can readily be generalised.

    Steve Luttrell
    West Malvern, UK
    Steve Luttrell, Nov 24, 2007
    1. Advertisements

  3. Peter

    Mark Fisher Guest

    Hi Peter,

    I'm not aware of any built-in function that does what you want. Here's
    something I wrote for myself that may be related to what you're
    looking for. The main function is MergeByKey. It calls a helper
    function SubsetPosition.


    (* code starts here *)

    MergeByKey::usage = "MergeByKey[{data1, data2, ...}, n] merges \
    the datasets treating the first n columns of each dataset \
    as the key. If n is omitted, the first column is used as the key. \
    MergeByKey[datasets, list] uses the list of column numbers to \
    specify the key columns which are placed first in the merged dataset.
    Only rows that have matching keys in all datasets appear in the \
    merged dataset. MergeByKey assumes there are no duplicate keys \
    in each of the datasets."

    SubsetPosition::usage = "SubsetPosition[set, subset] returns the \
    positions of subset in the set, assuming both set and subset are \
    sorted and have no duplicates."

    MergeByKey[datalist:{__List}, n_:1] :=
    Module[{i, keylist, ikeys, poslist, dlist},
    i = Range[n];
    keylist = #[[All, i]] & /@ datalist;
    ikeys = Intersection @@ keylist;
    poslist = SubsetPosition[#, ikeys]& /@ keylist;
    dlist = MapThread[#1[[#2, n + 1 ;;]] &, {datalist, poslist}];
    Join @@@ Transpose[Prepend[dlist, ikeys]]

    MergeByKey[datalist:{__List}, index_List] :=
    orderlist =
    Flatten[Join[index, Complement[Range[Length[#[[1]]]], index]]]& /@
    MergeByKey[MapThread[#1[[All, #2]]&, {datalist, orderlist}],

    SubsetPosition[superset:{__Integer}, subset:{__Integer}] :=
    sspCompiled[superset, subset]

    SubsetPosition[superset_, subset_] :=
    Module[{i = 1},
    (While[superset[] != #, i++]; i++) & /@ subset

    sspCompiled = Compile[{
    {superset, _Integer, 1},
    {subset, _Integer, 1}},
    Module[{i = 1},
    (While[superset[] != #, i++]; i++) & /@ subset

    (* code ends here *)
    Mark Fisher, Nov 24, 2007
  4. Peter

    Albert Guest

    Hi Peter,
    I also don't know about a built in function which would allow to do this
    and there is nothing wrong with the solutions that have been sent
    already. Still I couldn't resist to post another method which takes
    advantage of using lookup tables, which is something which comes, via
    pattern matching, as a built in to mathematica but is often not
    recognized as that. Here is a simple function that builds a lookup table
    from a list with keys from the n'th column of the list. Note that you
    will need a symbol which holds the lookup table in it's downvalues, so
    this is the first argument here:


    Note that it uses a list with zeros as "default" if no key is found. Of
    course you might want a different behavior concerning the defaults
    depending on your use case.

    now here are two lists:


    here I create two lookup tables using columns 1 respectively 4 as keys:


    these are all keys that are in either m1 or m2:


    build the merged lists using all these keys:


    Note how the defaults are used for keys not existing in one of the two

    Of course the details may vary and there are many possible variations.
    In general I found this a very useful programming pattern which allows
    me to write code which at least I can read easily and in many cases is
    also quite efficient, since the whole work of searching for keys is up
    to the pattern matcher, which is implemented at a low level and needs to
    do these things efficient to be of any use. Note that the keys are not
    limited at all but you can use any mathematica expression as keys, and
    by mixing with patterns you can implement defaults for non existing keys
    in a very powerful way. An earlier examination lead me to the conclusion
    that using plain strings as keys was very fast, while more complicated
    pattern were not so efficient, but this might have changed since version
    4.x ...

    When combining this to a function, you could use local variables to hold
    the lookup tables and which are cleaned up automatically:



    Albert, Nov 25, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.