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$
  • 1
    $\begingroup$ Look up UnsameQ[] (=!=). $\endgroup$ – J. M.'s technical difficulties Oct 15 '15 at 21:44
  • 1
    $\begingroup$ Unnecessary to write #["Factor"]; rather use #Factor, the short form. $\endgroup$ – m_goldberg Oct 15 '15 at 22:24
  • 1
    $\begingroup$ FWIW, in your second snippet, you should really be using SameQ[] (===). $\endgroup$ – J. M.'s technical difficulties Oct 15 '15 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$ – Mitchell Kaplan Oct 16 '15 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 Oct 16 '15 at 21:18
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|>}*)
| improve this answer | |
$\endgroup$
  • $\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$ – Albert Retey Oct 16 '15 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$ – Mitchell Kaplan Oct 16 '15 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$ – Albert Retey Oct 19 '15 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|>
}
| improve this answer | |
$\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.