# Conditional counting of array elements of mixed types

I am analyzing arrays (vectors) that have a mix of strings and numbers. The strings are the same, namely “N.A.” (as a result from some test).
The rest of the elements are numbers of which I would like to count how many x are in the range -1<=x<=1.

For instance, the array a might be:
a = [0.5, 1.33, 0.25, -1.23, “N.A.”, “N.A.”, -0.6]
I would like to count how many of the numbers are between -1 and +1.

I get an error for the impossibility to compare the value of a string and number, when I use:
count(i->(-1<=i<=1), a)

So, I would like the counter to ignore the strings.

An alternative is to filter:
count(i->(-1<=i<=1), filter!(x → x !=“N.A.”, a))

This gives a proper answer, but also removes all “N.A.” from the array, which is not what I want.

What would be the way to count numerical elements that meet the condition elegantly and at the same time leaving the array as is?
I look forward to your tips and explanations. Best regards.

To avoid the confusion: “The rest of the elements are numbers of which I would like to count how many elements a[i] are in the range -1<=a[i]<=1.”

Try this: `count(i -> (i isa Number && -1 <= i <= 1), a)`.

But generally using different types in a vector is an awful idea, because the performance will drop significantly. You can replace the strings with `NaN`s, which have the same meaning but are actually floating-point values, and `-1 <= i <= 1` will be automatically false for them.

1 Like

The filtering approach should work (but allocate additional memory) if you use `filter` instead of `filter!` but it’s better to just use a modified function to `count`, as already proposed.

quote=“aryavorskiy, post:3, topic:114516”]
count(i → (i isa Number && -1 <= i <= 1), a)
[/quote]

Thanks a lot! This works.
And yes, I recognize that mixing element types is not very nice. However, I am running Monte Carlo simulations and in some cases a numerical result is impossible, which we mark now with Not Applicable (“N.A.”) as we do not want to assign a fake number. But, I would welcome other work arounds.

Thanks a lot too. The filter function instead of filter! works very well too.

Also thanks for the NaN suggestion. I’ll adopt that in the simulations.