2
$\begingroup$

I need to delete all of the missing entries after executing this line:

data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[];
$\endgroup$
  • $\begingroup$ DeleteCases $\endgroup$ – Sektor Nov 17 '14 at 17:16
  • 3
    $\begingroup$ Did you try something? $\endgroup$ – gpap Nov 17 '14 at 17:19
7
$\begingroup$
data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[];

data2 = Select[data, FreeQ[#, _Missing] &];
data3 = DeleteCases[data, _?(! FreeQ[#, _Missing] &)];
data4 = Pick[data, FreeQ[#, _Missing] & /@ data];
data5  = (data /. {_Missing, _} | {_, _Missing} :> Sequence[]);
data6 = Cases[data, Except[{_Missing, _} | {_, _Missing}]];

Length /@ {data, data2, data3, data4, data5, data6}
(* {240, 228, 228, 228, 228, 228} *)
$\endgroup$
4
$\begingroup$

DeleteMissing is the built-in function for this task:

DeleteMissing[data, 1, 1]
$\endgroup$
2
$\begingroup$

I am adding this one for its novelty, but I would not use it in production as it is to uncontrolled:

Block[{Missing},
  Missing /: {_Missing, _} := Sequence[];
  Missing /: {_, _Missing} := Sequence[];
  data
] // Length
(* 228 *)

But, it operates by rewriting the behavior of Missing when it is found in a List.

$\endgroup$
1
$\begingroup$
Cases[data, _?(VectorQ[#, QuantityQ] &)] // Length
(*  228  *)

Caveat/limitation: The data, except for the Missing items, need to be quantities.

$\endgroup$
1
$\begingroup$

Similar to some of the answers by kguler, but using BlankSequence

data7 = data /. {___, _Missing, ___} :> Sequence[];
data8 = DeleteCases[data, {___, _Missing, ___}];
$\endgroup$
1
$\begingroup$

Why not simply:

DeleteCases[data,Missing[_],Infinity]

or, to delete the whole entry, if any part is missing:

DeleteCases[data,{Missing[_],_}|{_,Missing[_]},Infinity]
$\endgroup$
1
$\begingroup$

Simplest fastest way is:

DeleteCases[data, Missing[_]]

data can be a list of data or a function returning a list of data.

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