6
$\begingroup$

I would like to join associations in the following way. Say I have a dataset d1:

keys1 = {"first", "last", "age"}; first = {"Mike", "Carl", "Tina"}; last = {"Shulz", "Miller", "Ruben"}; age = {28, 35, 34}; 

then

d1 = Dataset[ AssociationThread[keys1, #] & /@ ({first, last, age}\[Transpose])] 

and a second dataset

names = d1[All, {"first", "last"}][All, #first ~~ " " ~~ #last &] // Normal (*{"Mike Shulz", "Carl Miller", "Tina Ruben"} *) 

and

income = {30000, 23000, 40000} 

so the dataset d2 is

d2 = Dataset[ AssociationThread[{"fullname", "income"}, #] & /@ ({names, income}\[Transpose])] 

Then I would like to combine (join) these datasets on the full name of a person, which is present in the second dataset but not in the first. Something like

JoinAcross[Normal@d1, Normal@d2, (Key["first"] ~~ Key["last"]) -> Key["fullname"]] 

(clearly) does not work. Is there an easy way to achieve the desired result (without modifying the first dataset and append a full name on it)?

Edit:

Besides the question is answered by a good workaround another questions arises: "Why the coloring"? I tried the solution from WReach - works fine, but I get parts of the answer colored and do not know why... Can someone help (again)?... Sorry, I just saw it: missing braces {}....

enter image description here

$\endgroup$

2 Answers 2

5
$\begingroup$

Unfortunately, JoinAcross does not presently (V11.1) support any form of computed key specification. We have no choice but to adjust the first dataset to include the full name.

We do not necessarily have to go so far as to destructively alter d1 -- we can add the computed column in an inline query:

JoinAcross[ d1[All, <| "fullname"-> #first~~" "~~#last, # |> &] // Normal , d2 // Normal , Key["fullname"] -> Key["fullname"] ] // Dataset 

dataset screenshot




As an aside, in later versions of Mathematica we can usually omit the applications of Normal, Key and Dataset:

JoinAcross[ d1[All, <| "fullname"-> #first~~" "~~#last, # |> &] , d2 , "fullname" -> "fullname" ] 

... but be prepared to add them back in since JoinAcross has been very finicky across releases (especially when applied to datasets).

$\endgroup$
1
  • $\begingroup$ Thank you for the fast answer, so I´m going to take this as a (quite good) workaround $\endgroup$ Commented May 30, 2017 at 5:28
1
$\begingroup$

In:

keys1 = {"first", "last", "age"}; first = {"Mike", "Carl", "Tina"}; last = {"Shulz", "Miller", "Ruben"}; age = {28, 35, 34}; d1 = Dataset[AssociationThread[keys1, #] & /@ ({first, last, age}\[Transpose])] fullname[person_] := "fullname" -> person["first"] <> " " <> person["last"] addFullname[person_] := Prepend[person, fullname@person] addFullnames[persons_] := persons // Normal // Map[addFullname] // Dataset addFullnames@d1 

Out: enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.