Given an Association list w/ a common Key but also some distinct ones:

data3 = {<|"commonKey" -> 1, "b" -> 3|>, <|"commonKey" -> 2, 
    "c" -> 4|>} // Dataset

enter image description here

How to implement logic similar to Switch and Which using named slots?

data3[All, Switch[#commonKey, 1, #b] &] // Normal

{3, Missing["Failed"]}

data3[All, Switch[#commonKey, 1, #b, 2, #c] &] //Normal 

{Missing["Failed"], Missing["Failed"]}

Similarly, using Which:

data3[All, Which[#commonKey == 1, #b, #commonKey == 2, #c] &]//Normal

{Missing["Failed"], Missing["Failed"]}

The cause is Function::slota Named Slot can't be filled from the association with the Key that is missing.

Is there an elegant workaround that can be mapped on existing Association list, as opposed to aggregating via GroupBy[#commonKey&]? (Reason: the actual data set that is simplified here is chronologically sorted, and grouping would not preserve the item sequence).

  • $\begingroup$ You really are trying to save time by abbreviating with w/ w/?? $\endgroup$
    – halirutan
    May 12, 2015 at 0:27
  • $\begingroup$ Programmable: w/, w/i, w/o ... $\endgroup$ May 12, 2015 at 18:03

1 Answer 1


You could use the whole association and access it inside your function:

data3[All, Switch[#commonKey, 1, #["b"], _, 0] &]

Mathematica graphics

Is this an alternative?

  • $\begingroup$ Not sure where the 0 comes from but did you mean to write this? data3[All, Switch[#commonKey, 1, #["b"], 2, #["c"]] &]? That works while Switch[#commonKey, 1, #b, 2, #c] . $\endgroup$ May 12, 2015 at 18:09
  • $\begingroup$ @alancalvitti I meant to write it because I thought you might want a fallback rule if there isn't any #b in the association. You can just remove the part and adapt if like you want. $\endgroup$
    – halirutan
    May 12, 2015 at 19:18
  • $\begingroup$ You may also be interested in this related q: mathematica.stackexchange.com/questions/83414/… $\endgroup$ May 14, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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