user73898
user73898

Reputation: 143

select string array values based on another array

I can select data that is equal to a value with

data = rand(1:3, 10)
value = 2
data .== value

or equal to a list of values with

values = [1, 2]
in.(data, (values,))

The last one is generic and also works for a scalar: in.(data, (value, )) .

However, this works for Int, but the generic does not work for String values:

data = rand(["A", "B", "C"], 10)
value = "B"
data .== value
values = ["A","B"]
in.(data, (values, ))
in.(data, (value, ))
ERROR: use occursin(x, y) for string containment

Is there a generic way for Strings?

For a generic val input I'm now writing the following, but I feel there must be a better solution.

isa(val, AbstractArray) ? in.(data, (val,)) : data .== val

Background: I'm creating a function to select rows from a dataframe (and do something with them) but I want to allow for both a list of values as well as a single value.

Upvotes: 1

Views: 397

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69879

Here is a trick that is worth knowing:

[x;]

Now - if x is an array it will remain an array. If x is a scalar it will become a 1-element array. And this is exactly what you need.

So you can write

in.(data, ([val;],))

The drawback is that it allocates a new array, but I guess that val is small and it is not used in performance critical code? If the code is performance critical I think it is better to treat scalars and arrays by separate branches.

Upvotes: 2

Related Questions