2
$\begingroup$
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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Sjoerd Smit May 1 at 13:22
  • $\begingroup$ 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 $\endgroup$ – Annesha Bhoumik May 1 at 13:30
  • 1
    $\begingroup$ How about Keys @ MinimalBy[KeyDrop[#, {"userId", "MinPerf"}], Identity]? $\endgroup$ – Sjoerd Smit May 1 at 13:35
  • $\begingroup$ Perfect. Works. Thank You $\endgroup$ – Annesha Bhoumik May 1 at 13:38
  • $\begingroup$ @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 $\endgroup$ – Annesha Bhoumik May 1 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.