# Retaining rows with only non-zero values in a large table

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?

• 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? Commented Dec 3, 2015 at 12:04
• @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. Commented Dec 3, 2015 at 13:23
• I'm quite sure that this is a duplicate, but I cannot find it right now. Commented Dec 3, 2015 at 14:26
• @Yves Klett, I also looked over the forum, but without finding any other reference to a similar problem. Commented Dec 3, 2015 at 14:36
• @Xavier_B o. K. - I may well be mistaken... thank you for looking first in any case! Commented Dec 3, 2015 at 18:26

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


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.

• This works like a charm! Exactly what I needed! Thanks! Commented Dec 3, 2015 at 14:25
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}}