4
$\begingroup$

I have

   mydata= {{{2017, 1, 1}, 116.461, 61.7762, 65.4011},
           {{2017, 1, 8}, 117.577, 61.6386, "NA"},
          {{2017, 1, 15}, 118.525, 61.6779, 45.62},
          {{2017, 1, 22}, 120.451, "NA", "NA"},
          {{2017, 1, 29}, 127.494, 62.602, 85.25},
          {{2017, 1, 29}, "NA", 62.602, 88.25}};

I am looking for different ways to delete all data points of all rows that have at least one "NA". My result should be as fallow:

myresult = {{{2017, 1, 1}, 116.461, 61.7762, 65.4011},
            {{2017, 1, 15}, 118.525, 61.6779, 45.62},
            {{2017, 1, 29},127.494, 62.602, 85.25}};

Although this example has just a few rows and a few columns, my actual data has many more rows and columns. Here is related question but answer was not much helpful for me. Here is related question from stackoverflow.com. Any help is greatly appreciated.

$\endgroup$
  • $\begingroup$ See Select and FreeQ. If you need better performance the three argument form of FeeQ may help. $\endgroup$ – b3m2a1 Sep 2 '17 at 23:41
  • $\begingroup$ another option Pick[mydata,Not[MemberQ[#,"NA"]]&/@mydata] $\endgroup$ – Nasser Sep 2 '17 at 23:44
5
$\begingroup$

In decreasing order of brevity:

mydata // Select[FreeQ["NA"]]

Cases[mydata, Except[{___, "NA", ___}]]

Delete[mydata, {#} & @@@ Position[mydata, "NA"]]

Slightly more involved to set up, but convenient afterward:

{___, kill[], ___} ^:= Sequence[]

mydata /. "NA" -> kill[]

Related:

$\endgroup$
  • $\begingroup$ @ Mr. Wizard, thank your for your answer. All these answers are very helpful. $\endgroup$ – ramesh Sep 3 '17 at 15:12
4
$\begingroup$
Select[mydata, ! MemberQ[#, "NA"] &]
$\endgroup$
  • $\begingroup$ @ David, thank you for your time and answer. I think it addresses my concern. $\endgroup$ – ramesh Sep 3 '17 at 0:16
4
$\begingroup$
Cases[mydata, {_, __?NumericQ}]

Or

DeleteCases[mydata, {___, _String, ___}]

Or

Replace[mydata, {___, _String, ___} :> Sequence[], {1}]

{{{2017, 1, 1}, 116.461, 61.7762, 65.4011}, {{2017, 1, 15}, 118.525, 61.6779, 45.62}, {{2017, 1, 29}, 127.494, 62.602, 85.25}}

$\endgroup$
  • $\begingroup$ @ eldo, thank you for your time and answer. I have tried doing this but I was not sure whether it would be general enough to handle any number of columns. I think it can handle any number of columns. $\endgroup$ – ramesh Sep 3 '17 at 0:25
3
$\begingroup$

If you're dataset used Missing["NA"] you could just use DeleteMissing. Instead, I will replace "NA" with Missing["NA"] and then use DeleteMissing:

DeleteMissing[
    Replace[mydata, "NA"->Missing["NA"], {2}],
    1,
    Infinity
]

{{{2017, 1, 1}, 116.461, 61.7762, 65.4011}, {{2017, 1, 15}, 118.525, 61.6779, 45.62}, {{2017, 1, 29}, 127.494, 62.602, 85.25}}

$\endgroup$
  • $\begingroup$ @ Carl, thank you for your time and answer. I think it addresses my concern. $\endgroup$ – ramesh Sep 3 '17 at 0:16
3
$\begingroup$
mydata /. {___, _String, ___} :> Nothing
$\endgroup$
  • $\begingroup$ @ jiaoeyushushu, thank you for your time and answer. $\endgroup$ – ramesh Sep 3 '17 at 15:29

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.