Given an Association
list w/ a common Key
but also some distinct ones:
data3 = {<|"commonKey" -> 1, "b" -> 3|>, <|"commonKey" -> 2,
"c" -> 4|>} // Dataset
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).