I need to delete all of the missing entries after executing this line:
data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[];
I need to delete all of the missing entries after executing this line:
data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[];
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} *)
DeleteMissing is the built-in function for this task:
DeleteMissing[data, 1, 1]
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.
Simplest fastest way is:
DeleteCases[data, Missing[_]]
data can be a list of data or a function returning a list of data.
Cases[data, _?(VectorQ[#, QuantityQ] &)] // Length
(* 228 *)
Caveat/limitation: The data, except for the Missing items, need to be quantities.
Similar to some of the answers by kguler, but using BlankSequence
data7 = data /. {___, _Missing, ___} :> Sequence[];
data8 = DeleteCases[data, {___, _Missing, ___}];
Why not simply:
DeleteCases[data,Missing[_],Infinity]
or, to delete the whole entry, if any part is missing:
DeleteCases[data,{Missing[_],_}|{_,Missing[_]},Infinity]
DeleteCases– Sektor Nov 17 '14 at 17:16