# Finding associations not having value Missing[“Unmatched”] in a list of associations

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?

• Look up UnsameQ[] (=!=). – J. M.'s technical difficulties Oct 15 '15 at 21:44
• Unnecessary to write #["Factor"]; rather use #Factor, the short form. – m_goldberg Oct 15 '15 at 22:24
• FWIW, in your second snippet, you should really be using SameQ[] (===). – J. M.'s technical difficulties Oct 15 '15 at 23:05
• @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? – Mitchell Kaplan Oct 16 '15 at 14:12
• 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. – m_goldberg Oct 16 '15 at 21:18

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|>}*)

• 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... – Albert Retey Oct 16 '15 at 14:37
• @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! – Mitchell Kaplan Oct 16 '15 at 21:24
• @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... – Albert Retey Oct 19 '15 at 9:33

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|>
}