8
$\begingroup$

I have a list like this (experimental data):

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, {0.04,22.4, 43.289, 99.997}, {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, {588.24, "", "",76.519}, {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, {},{0.05, 22.4, 43.293, 99.997}}

I want to remove every sublist that has a missing value, so if a sublist is like {588.24, "", "", 76.519}, it should be removed, it is like {}, it should be removed. Can this be done in a simple way?

$\endgroup$
1
  • $\begingroup$ Try: Map[If[MemberQ[#, ""] || # == {}, Nothing, #] &, data, 1] $\endgroup$ Commented Aug 26, 2022 at 6:43

6 Answers 6

9
$\begingroup$

I want to remove every sublist that has a missing value

There are at least 10 ways to do this in Mathematica. One way could be

data = {{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 
   99.997}, {0.04, 22.4, 43.289, 99.997}, {588.24, "", "", 
   76.519}, {588.24, "", "", 76.519}, {588.24, "", "", 
   76.519}, {588.24, "", "", 76.519}, {588.24, "", "", 
   76.519}, {}, {0.05, 22.4, 43.293, 99.997}}

DeleteCases[data, x_ /; Or[MemberQ[x, ""], Length[x] == 0]]

gives

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, {0.04, 
  22.4, 43.289, 99.997}, {0.05, 22.4, 43.293, 99.997}}
$\endgroup$
8
$\begingroup$

You can match the data you need

data = {{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 
    99.997}, {0.04, 22.4, 43.289, 99.997}, {588.24, "", "", 
    76.519}, {588.24, "", "", 76.519}, {588.24, "", "", 
    76.519}, {588.24, "", "", 76.519}, {588.24, "", "", 
    76.519}, {}, {0.05, 22.4, 43.293, 99.997}};
data // Cases[{_Real ..}]
$\endgroup$
5
$\begingroup$
Pick[data, FreeQ[#, ""] & /@ data] /. {} -> Nothing

OR

pos = Position[data, x_ /; Length[x] > 0 && FreeQ[x, ""]]
Extract[pos][data]

Result:

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, {0.04, 
  22.4, 43.289, 99.997}, {0.05, 22.4, 43.293, 99.997}}
$\endgroup$
5
$\begingroup$

Another way is as follows:

DeleteCases[Map[If[NumericQ[#] === True, #, Nothing] &, data, {-1}], {}]

(*{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, {0.04, 22.4, 43.289, 99.997}, {588.24, 76.519}, {588.24, 76.519}, {588.24, 76.519}, {588.24, 76.519}, {588.24, 76.519}, {0.05, 22.4, 43.293, 99.997}}*)
$\endgroup$
2
$\begingroup$
list =
  {{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, 
   {0.04, 22.4, 43.289, 99.997}, {588.24, "", "", 76.519}, 
   {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, 
   {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, 
   {}, {0.05, 22.4, 43.293, 99.997}};

Three more solutions / modifications

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[list, {{___, "", ___} | {}}]

Using DeleteCases with OrderlessPatternSequence

DeleteCases[list, {OrderlessPatternSequence[__, ""]} | {}]

Using Cases

Cases[list, {__?NumberQ}]

All produce

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, {0.04, 22.4, 43.289, 99.997}, {0.05, 22.4, 43.293, 99.997}}

$\endgroup$
2
$\begingroup$
list =
  {{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, 
   {0.04, 22.4, 43.289, 99.997}, {588.24, "", "", 76.519}, 
   {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, 
   {588.24, "", "", 76.519}, {588.24, "", "", 76.519}, 
   {}, {0.05, 22.4, 43.293, 99.997}};

Using Replace at level 1:

Replace[data, s : Except[{__?NumberQ}] :> Nothing, {1}]

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997},
{0.04, 22.4, 43.289, 99.997}, {0.05, 22.4, 43.293, 99.997}}

Or using Replace at level 2 with Cases:

Cases[{__}]@Replace[data, s : Except[_?NumberQ] :> Nothing, {2}]

{{0.04, 22.4, 43.286, 99.998}, {0.04, 22.4, 43.288, 99.997}, 
{0.04, 22.4, 43.289, 99.997}, {588.24, 76.519}, {588.24, 76.519}, 
{588.24, 76.519}, {588.24, 76.519}, {588.24, 76.519},
{0.05, 22.4, 43.293, 99.997}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.