3
$\begingroup$

I have a Dataset[] with some data and a TON of columns. Each row contains several columns for dates, which contain DateObject values, but they aren't always populated. If a value is Null, I've replaced it with Missing[] for a) prettier display in Dataset visualization, b) to allow use of MissingQ and DeleteMissing.

Here's my question. I am trying to get a single definitive date for each row based on the first non-Missing value. It's possible that ALL values will be missing. In that case either nothing needs to be returned or a Missing["No date"] is fine.

What I have so far is very ugly and crude, and I'm wondering if there's a more elegant way to do this, that simply escapes me for now?

Flatten[
  Take[ DeleteMissing@#, UpTo[1] ]& /@
  Normal@Values@encounters[ All,
    {"ArriveDTS", "EstimatedArriveDTS", "RegistrationDTS", 
     "TriageDTS", "InpatientAdmitDTS", "AssignToLocationDTS", 
     "DischargeDTS", "DepartDTS", "EstimatedDepartDTS", 
     "PreRegistrationDTS"}
  ]
]
$\endgroup$
  • 2
    $\begingroup$ SelectFirst[Not @* MissingQ]? $\endgroup$ – Pillsy Apr 20 '17 at 0:48
  • $\begingroup$ @Pillsy Please add as an answer. That worked wonders! Thank you! Finished product: encounters[All, SelectFirst[Not@*MissingQ]@*KeyTake[{"ArriveDTS", "EstimatedArriveDTS", "RegistrationDTS", "TriageDTS", "InpatientAdmitDTS", "AssignToLocationDTS", "DischargeDTS", "DepartDTS", "EstimatedDepartDTS", "PreRegistrationDTS"}]] $\endgroup$ – Gregory Klopper Apr 20 '17 at 1:47
2
$\begingroup$

The SelectFirst function does exactly what you want. As you note in your comment ( :) ), this will work:

encounters[All, SelectFirst[Not@*MissingQ]@*KeyTake[{"ArriveDTS",  
  "EstimatedArriveDTS", "RegistrationDTS", "TriageDTS", "InpatientAdmitDTS",   
  "AssignToLocationDTS", "DischargeDTS", "DepartDTS", "EstimatedDepartDTS", 
  "PreRegistrationDTS"}]]
$\endgroup$

Your Answer

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

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