1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.