4
$\begingroup$

I have a list of pairs of coordinates, where some are missing.

coordinates = {
  {{1, 1}, {1, 2}},
  {{3, 4}, {5, 5}},
  {{1, 2}, Missing[]},
  {{9, 8}, {7, 7}},
  {Missing[], {2, 3}}
}

I am trying to remove the pairs containing missing coordinates, to get

coordinates = {
  {{1, 1}, {1, 2}},
  {{3, 4}, {5, 5}},
  {{9, 8}, {7, 7}},
}

I tried

coordinates = DeleteCases[coordinates, MemberQ[#, _Missing] &]

but that did not work. But the function MemberQ works on pairs directly:

MemberQ[coordinates[[1]], _Missing] (* returns False *)
MemberQ[coordinates[[3]], _Missing] (* returns True *)

What did I do wrong?

$\endgroup$
  • 1
    $\begingroup$ DeleteCases[coordinates, _?(MemberQ[#, _Missing] &)] or DeleteCases[coordinates, x_ /; MemberQ[x, _Missing]] work $\endgroup$ – andre314 Jun 6 '14 at 17:32
  • $\begingroup$ Related: (1234), (2655), (29159) $\endgroup$ – Mr.Wizard Jun 7 '14 at 14:21
5
$\begingroup$
coordinates = {{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{1, 2}, Missing[]}, {{9, 8}, {7, 7}}, {Missing[], {2, 3}}};

The second argument to DeleteCases must be a pattern not a boolean.

correctedCoordinates = DeleteCases[coordinates, _?(MemberQ[#, _Missing] &)]

{{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{9, 8}, {7, 7}}}

Similarly with Cases

correctedCoordinates == Cases[coordinates, _?(FreeQ[#, _Missing] &)]

True

Whereas, Select requires a boolean.

correctedCoordinates == Select[coordinates, FreeQ[#, _Missing] &]

True

$\endgroup$
  • $\begingroup$ Yes, you're right. I was mistaken about what was going wrong. Do you know what happened to MathGroup? No new messages since May 22. $\endgroup$ – Szabolcs Jun 6 '14 at 18:31
  • $\begingroup$ Presumably, most/all of the traffic migrated here and the moderator fell asleep. :-) $\endgroup$ – Bob Hanlon Jun 6 '14 at 18:54
3
$\begingroup$
DeleteCases[coordinates, {___, Missing[], ___}]

Or (slower)

Cases[coordinates, {{__?NumberQ}, {__?NumberQ}}]

I like this style because of its flexibility in more complicated cases.

With

x = Join @@ Table[coordinates, {10^5}];

Eldo DeleteCases : 0.22 sec.
Szabolcs: : 0.86 sec.
Bob Hanlon DeleteCases : 0.95 sec.

$\endgroup$
3
$\begingroup$

Bob Hanlon has the answer, but merely as a supplement: if your data is as you show I would simply use Select and MatrixQ.

Select[coordinates, MatrixQ]
{{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{9, 8}, {7, 7}}}
$\endgroup$
0
$\begingroup$

You could also use DeleteMissing:

DeleteMissing[coordinates, 1, 1]

{{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{9, 8}, {7, 7}}}

but it's more than 4 times slower on my PC than the solution provided in the answer by Mr.Wizard.

$\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.