5
$\begingroup$

How can I remove those rows in a table that have a missing element.

For a contrived example:

Table[{If[i == 1, Missing[NotApplicable], i], j}, {i, 1, 2}, {j, 1, 
   2}] // TableForm

Gives:

enter image description here

But I want is:

enter image description here

In the real example there is is more complex function that sometimes returns missing for some values, and I want those rows excluded.

Ideally, if the first element in the row is missing, the rest of the elements in the row would not be processed, but that is just a performance bonus.

How can I do this?

$\endgroup$
  • 2
    $\begingroup$ Have you tried DeleteMissing[expr, 1, Infinity]? This should remove any elements at the first level that contain Missing at any depth. It's one of the syntax examples in the "Details" section of the docs for DeleteMissing. $\endgroup$ – MarcoB Nov 10 '16 at 4:57
  • $\begingroup$ Very handy. Is there a way to get this to work with a table? The table currently has headers that I want to keep $\endgroup$ – soandos Nov 10 '16 at 4:59
  • 1
    $\begingroup$ If by table you mean the output of TableForm, that won't work in all likelihood. in general TableForm is for output formatting only. Its output cannot be used for further computation. $\endgroup$ – MarcoB Nov 10 '16 at 5:05
  • $\begingroup$ Yes. So what is the right way to do what I am looking for? I am happy to convert it to a dataset or something else if that would be easier. So long as I have the headers (row and column) its not a big deal what it looks like exactly $\endgroup$ – soandos Nov 10 '16 at 5:08
  • 1
    $\begingroup$ A Dataset would probably be the most appropriate construct; a quick and dirty Grid might also work for formatting. $\endgroup$ – MarcoB Nov 10 '16 at 5:12
6
$\begingroup$

Try Cases.

tab = {{Missing[NotApplicable], 1},{2, 3},{4, 5}};
Cases[tab, Except[{___, Missing[___], ___}]]
(*{{2, 3}, {4, 5}}*)

The pattern Except[{___, Missing[___], ___}] matches anything that's not a list containing an element of head Missing.

$\endgroup$
  • $\begingroup$ I just realized, this is coming from a table with headers. Is there a way to do this either with the table generation, or get the headers to work with the new data (i.e. delete the headers that are no longer valid) $\endgroup$ – soandos Nov 10 '16 at 4:59
1
$\begingroup$

Perhaps simply this

 t=Table[{If[i == 1, Missing[NotApplicable], i], j}, {i, 1, 2}, {j, 
 1, 2}]
 Drop[t, 1, None] // TableForm
$\endgroup$
1
$\begingroup$
expr = {{{{{Missing[NotApplicable]}}}, 1}, {2, 3}, {4, 5}};

For versions 10.0+, DeleteMissing[expr, 1, Infinity] gives the desired result as noted by MarcoB in the comments.

DeleteMissing[expr, 1, Infinity]

{{2, 3}, {4, 5}}

For earlier versions you can use

Select[expr, FreeQ[#, _Missing] &] (* or *)
DeleteCases[expr, _?(Not[FreeQ[#, _Missing]] &)]

{{2, 3}, {4, 5}}

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