4
$\begingroup$

I have a list of Associations, a:

{<|"Year" -> 2011, "Loss" -> 141972., "Factor" -> 1.02884|>, 
 <|"Year" -> 2009, "Loss" -> 27081., "Factor" -> 1.01863|>,
 <|"Year" -> 2014, "Loss" -> 770.693, "Factor" -> Missing["Unmatched"]|>, 
 <|"Year" -> 2014, "Loss" -> 173.242, "Factor" -> Missing["Unmatched"]|>,
 <|"Year" -> 2010, "Loss" -> 163125., "Factor" -> 1.37016|>}

If I look for the rows with Missing["Unmatched"], I'm successful:

Select[a, #["Factor"] == Missing["Unmatched"] &]

{<|"Year" -> 2014, "Loss" -> 770.693, "Factor" -> Missing["Unmatched"]|>,
 <|"Year" -> 2014, "Loss" -> 173.242, "Factor" -> Missing["Unmatched"]|>}

But if I try to find the ones that have values, I'm unsuccessful:

Select[a, #["Factor"] != Missing["Unmatched"] &]
{}

I can do it by looking for numbers:

Select[a, NumberQ[#["Factor"]] &]

{<|"Year" -> 2011, "Loss" -> 141972., "Factor" -> 1.02884|>,
 <|"Year" -> 2009, "Loss" -> 27081., "Factor" -> 1.01863|>,
 <|"Year" -> 2010, "Loss" -> 163125., "Factor" -> 1.37016|>}

But I'd like to be able to get the complement of the matches, just in case there's something I need to see that's not a number. What am I missing?

$\endgroup$
5
  • 1
    $\begingroup$ Look up UnsameQ[] (=!=). $\endgroup$ Commented Oct 15, 2015 at 21:44
  • 1
    $\begingroup$ Unnecessary to write #["Factor"]; rather use #Factor, the short form. $\endgroup$
    – m_goldberg
    Commented Oct 15, 2015 at 22:24
  • 1
    $\begingroup$ FWIW, in your second snippet, you should really be using SameQ[] (===). $\endgroup$ Commented Oct 15, 2015 at 23:05
  • $\begingroup$ @m_goldberg Sometimes I have key names with spaces in them. Maybe that's not a good idea. But in any case, I've gotten used to using #["KeyName"]. Can I avoid this in the case of key names with spaces? $\endgroup$ Commented Oct 16, 2015 at 14:12
  • 1
    $\begingroup$ Keys with spaces are bad idea only if you want to be able to take advantage of the short form. Which is more valuable to you: using phrases for keys or being able to make short form references to keys? Go with what you value most. But you can't have both for the same key. $\endgroup$
    – m_goldberg
    Commented Oct 16, 2015 at 21:18

2 Answers 2

8
$\begingroup$

You want MissingQ here, or as J.M. says UnsameQ.

Select[a, !MissingQ[#Factor] & ]

(*{<|"Year" -> 2011, "Loss" -> 141972., 
  "Factor" -> 1.02884|>, <|"Year" -> 2009, "Loss" -> 27081., 
  "Factor" -> 1.01863|>, <|"Year" -> 2010, "Loss" -> 163125., 
  "Factor" -> 1.37016|>}*)
$\endgroup$
3
  • $\begingroup$ Although the OP accepted this, I think the version with UnsameQ would be a more correct answer to the question as it is formulated. Consider an entry which has "Factor" ->Missing["OtherReason"]. With MissingQ one would not see that in the result, with #Factor=!=Missing["Unmatched"] one would see it, and that would be what I understand the question asks for. So I think you probably should a) give an explict example for the UnsameQ variant also and b) explain the difference between the two... $\endgroup$ Commented Oct 16, 2015 at 14:37
  • $\begingroup$ @AlbertRetey you're right, although in my particular case I really needed to see what was missing for whatever reason. However, I do agree with you that I could be misled as to what was actually happening with my data. Thanks! $\endgroup$ Commented Oct 16, 2015 at 21:24
  • $\begingroup$ @MitchellKaplan: I just have addd the UnsameQ example for completeness (as comments should not be considered to be persistent). I think you should not change the acceptance, my answer is more for those readers who are interested in filtering out specific cases of Missing only and happen to find that question... $\endgroup$ Commented Oct 19, 2015 at 9:33
1
$\begingroup$

just for completeness here is the explicit example of how to use UnsameQ (=!=) in such a way that other appearacnes of Missing with different arguments will be returned:

Select[a, #Factor =!= Missing["Unmatched"] &]

use this example to see the difference to choys answer:

a = {
  <|"Year" -> 2011, "Loss" -> 141972.,"Factor" -> 1.02884|>,
  <|"Year" -> 2009, "Loss" -> 27081.,"Factor" -> 1.01863|>,
  <|"Year" -> 2014, "Loss" -> 770.693,"Factor" -> Missing["Unmatched"]|>,
  <|"Year" -> 2014, "Loss" -> 173.242,"Factor" -> Missing["Unmatched"]|>,
  <|"Year" -> 2012, "Loss" -> 10.345, "Factor" -> Missing["NotAvailable"]|>,
  <|"Year" -> 2010, "Loss" -> 163125., "Factor" -> 1.37016|>
}
$\endgroup$

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.