3

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

data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[];
Sektor
  • 3,320
  • 7
  • 27
  • 36
gotwals
  • 127
  • 4

7 Answers7

8
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} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
5

DeleteMissing is the built-in function for this task:

DeleteMissing[data, 1, 1]
Karsten7
  • 27,448
  • 5
  • 73
  • 134
3

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.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
2

Simplest fastest way is:

DeleteCases[data, Missing[_]]

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

2
Cases[data, _?(VectorQ[#, QuantityQ] &)] // Length
(*  228  *)

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747
2

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

data7 = data /. {___, _Missing, ___} :> Sequence[];
data8 = DeleteCases[data, {___, _Missing, ___}];
Karsten7
  • 27,448
  • 5
  • 73
  • 134
2

Why not simply:

DeleteCases[data,Missing[_],Infinity]

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

DeleteCases[data,{Missing[_],_}|{_,Missing[_]},Infinity]
Jinxed
  • 3,753
  • 10
  • 24