# If both A and B columns of a dataset satisfy condition X, then create another column with a value A,B

data = Dataset[{<|"userId" -> 5324,
"PerfFreeResponse" -> 0.8181818181818182,
"PerfMultipleChoice" -> 0.5, "PerfSingleChoice" -> 0.5,
"PerfWriteCode" -> 1., "MinPerf" -> 0.5|>, <|"userId" -> 5325,
"PerfFreeResponse" -> 1., "PerfMultipleChoice" -> 1.,
"PerfSingleChoice" -> 1., "PerfWriteCode" -> 1.,
"MinPerf" -> 1.|>, <|"userId" -> 5326,
"PerfFreeResponse" -> 0.9090909090909091,
"PerfMultipleChoice" -> 0.6666666666666666,
"PerfSingleChoice" -> 1., "PerfWriteCode" -> 1.,
"MinPerf" -> 0.6666666666666666|>, <|"userId" -> 5330,
"PerfFreeResponse" -> 0.6363636363636364,
"PerfMultipleChoice" -> 0.6666666666666666,
"PerfSingleChoice" -> 1., "PerfWriteCode" -> 1.,
"MinPerf" -> 0.6363636363636364|>, <|"userId" -> 5331,
"PerfFreeResponse" -> 0.6363636363636364,
"PerfMultipleChoice" -> 1., "PerfSingleChoice" -> 1.,
"PerfWriteCode" -> 0., "MinPerf" -> 0.|>}]


I want to append another column which tells me which type of question (Free response, Multiple Choice, Single Choice, write Code) has the minimum score. I wrote the following code for that:

  data[All, Append[#, "MinPerfType" ->
If[#MinPerf == 1., "Excellent Performance",
If[#MinPerf == #PerfFreeResponse, "Free Response",
If[#MinPerf == #PerfMultipleChoice, "Multiple Choice",
If[#MinPerf == #PerfSingleChoice, "Single Choice",
If[#MinPerf == #PerfWriteCode, "Write Code", "Blah"]]]]]] &]


But the problem with this code is, if two columns show the MinPerf (except when MinPerf==1.), it gives me the name of only one column and not both of them. Eg., the first row of data shows MinPerf = 0.5, which is true for both Multiple Choice and Single Choice. In that case, I want MinPerfType to show both Multiple Choice and Single Choice and not just either one of them.

• The first advice I would give you, is to look up Which to handle the cascading conditions. Also, wouldn't MinimalBy be useful for this? MinimalBy[assoc, Identity] returns an association containing all keys that have minimal values. – Sjoerd Smit May 1 '19 at 13:22
• Thank you for your comment. I used MinimalBy and wrote this: data[All, Append[#, "MinPerfType" -> If[#MinPerf == 1., "Excellent Perf", MinimalBy[{#PerfFreeResponse, #PerfMultipleChoice, \ #PerfSingleChoice, #PerfWriteCode}, #MinPerf]]] &]. But I need names corresponding to the columns that have the minimum score. MinimalBy satisfies the condition of giving two minimum values but not the corresponding names – Priyesh Sharma May 1 '19 at 13:30
• How about Keys @ MinimalBy[KeyDrop[#, {"userId", "MinPerf"}], Identity]`? – Sjoerd Smit May 1 '19 at 13:35
• Perfect. Works. Thank You – Priyesh Sharma May 1 '19 at 13:38
• @SjoerdSmit How can I get the elements in string form rather than list form like PerfMultipleChoice, PerfSingleChoice rather than {"PerfMultipleChoice","PerfSingleChoice "}? I tried ToString and then StringDelete, but that doesn't work – Priyesh Sharma May 1 '19 at 15:36