Message Boards Message Boards

0
|
807 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Delete Cases of an Expression Containing Missing[]

Posted 5 months ago

Hello everyone,

I have a 4x3 matrix called myMatrix. The elements are either lists, e.g., {-5} or {-5,4}, or the result of subtraction that failed because the first number was missing from the association from which it was obtained, e.g., Missing["KeyAbsent", "2,838,683"] - 13.

 myMatrix = ({
      {{-5}, Missing["KeyAbsent", "2,838,683"] - 13, 
       Missing["KeyAbsent", "2,028,297"] - 13},
      {{-5, 4}, Missing["KeyAbsent", "2,838,683"] - 13, 
       Missing["KeyAbsent", "2,028,297"] - 13},
      {{-5}, Missing["KeyAbsent", "2,838,683"] - 13, 
       Missing["KeyAbsent", "2,028,297"] - 13},
      {Missing["KeyAbsent", "2,841,787"] - 18, {-5}, {2}}
     })

I want to delete the elements (or replace them) containing Missing but can't figure out how to do it.

DeleteCases[myMatrix, _Missing - 13, {2}]

works for those elements where 13 was to be subtracted; likewise for 18. Then why doesn't

DeleteCases[myMatrix, _Missing - _Integer, {2}]

work for any element?

Any tip would be much appreciated.

Greg

POSTED BY: Gregory Lypny
2 Replies
Posted 5 months ago

Excellent. Much obliged, Eric. Never thought of mapping FreeQ over the sub-elements to look for Missing. And thanks for the insight on FullForm.

Greg

POSTED BY: Gregory Lypny
Posted 5 months ago

You didn't provide an expected output, so I'm guessing. Does this give you what you want?

Select[FreeQ[_Missing]] /@ myMatrix
(* {{{-5}}, {{-5, 4}}, {{-5}}, {{-5}, {2}}} *)

To understand why

DeleteCases[myMatrix, _Missing - _Integer, {2}]

doesn't work, look at the full form of the pattern and the expressions that you want to match:

_Missing - _Integer // FullForm
(* Plus[Times[-1, Blank[Integer]], Blank[Missing]] *)

-13 + Missing["KeyAbsent", "2,838,683"] // FullForm
(* Plus[-13, Missing["KeyAbsent", "2,838,683"]] *)

So, structurally, the expressions don't match. You need to remember that everything is an expression. You also need to remember that Mathematica will normalize arithmetic expressions in ways that are not necessarily intuitive.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract