2
$\begingroup$

I have a long table with chronological data over several variables. In some instances, the data are missing, replaced by "NA". Example:

2003-W01,NA,NA,NA,NA,NA,NA,0,1,2,2,7.593746345,86.84550216,13.223286,4.245984537

I want to be able to do computations on the elements under condition that none are negative or unavalailable.

How can I do this?

$\endgroup$
5
  • 1
    $\begingroup$ It would be helpful, if you add a slightly more extended sample of data. As I understand, this is an example of a single row? What do you want to do with rows, that have unavailable elements? Simply drop them? $\endgroup$
    – LLlAMnYP
    Commented Dec 3, 2015 at 12:04
  • $\begingroup$ @LLlAMnYP, yes, simply drop them. I have more than enough. Yes, the data sample I provided is one row(part of it), all the rows are along the same pattern. $\endgroup$
    – Xavier_B
    Commented Dec 3, 2015 at 13:23
  • $\begingroup$ I'm quite sure that this is a duplicate, but I cannot find it right now. $\endgroup$
    – Yves Klett
    Commented Dec 3, 2015 at 14:26
  • $\begingroup$ @Yves Klett, I also looked over the forum, but without finding any other reference to a similar problem. $\endgroup$
    – Xavier_B
    Commented Dec 3, 2015 at 14:36
  • $\begingroup$ @Xavier_B o. K. - I may well be mistaken... thank you for looking first in any case! $\endgroup$
    – Yves Klett
    Commented Dec 3, 2015 at 18:26

2 Answers 2

2
$\begingroup$

The sample code for such task may be:

(*creating sample data with 10 rows and 5 columns*)
data = RandomReal[{-10, 10}, {10, 5}];
data[[RandomInteger[{1, 10}, 3], RandomInteger[{1, 5}, 3]]] = 0
data[[RandomInteger[{1, 10}, 3], RandomInteger[{1, 5}, 3]]] = "NA"
data // TableForm (*display output*)
(*the part of removing*)
toRemove = 
  Union[Position[data, 0][[All, 1]], Position[data, "NA"][[All, 1]]];
toBeLeft = Complement[Range[1, Length[data]], toRemove];
data=data[[toBeLeft]] 
data// TableForm

or shorter:

helper[d_] := AllTrue[d, ! (# == 0 || # == "NA") &]
dt = Select[data, helper[#] &]
dt // TableForm

example outcome

If there is only one row or column, then

dt=Select[data, ! (# == 0 || # == "NA") &]
dt//TableForm

In general take a look at functions Cases[], Select[], Drop[] and similar.

$\endgroup$
1
  • $\begingroup$ This works like a charm! Exactly what I needed! Thanks! $\endgroup$
    – Xavier_B
    Commented Dec 3, 2015 at 14:25
1
$\begingroup$
ClearAll@posi
posi[0 | 0.0] := True
posi[x_?Positive] := True
posi[x_] := False

list = 
  {{"W01", NA, NA, NA}, {1, 2.3, 5, 0}, {1, -2, 3, 7.59}, {86.8, 13.2, 4.24, 8}};

Drop all rows containing negative numbers or non-numerical elements

sel = Select[list, And @@ Map[posi, #] &]

{{1, 2.3, 5, 0}, {86.8, 13.2, 4.24, 8}}

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