Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Index of a pattern in a String vector with missing values in Julia

I am attempting to retrieve the index of all instances of a pattern in a String vector with missing values.

E.g. How can I get a vector with the index of all instances containing the pattern "a" from:

x = ["ab", "ca", "bc", missing, "ad"]

The desired outcome would be equal to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Vector([1, 2, 5])
3-element Vector{Int64}:
 1
 2
 5

As these are the indexes in which the pattern appears.

>Solution :

A natural way to write it is:

julia> findall(v -> ismissing(v) ? false : contains(v, "a"), x)
3-element Vector{Int64}:
 1
 2
 5

Alternatively you could write:

julia> using Missings

julia> findall(coalesce.(passmissing(contains).(x, "a"), false))
3-element Vector{Int64}:
 1
 2
 5

which in this case is less readable, but in other contexts you might find passmissing and coalesce useful so I mention them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading