Home > Net >  Index of a pattern in a String vector with missing values in Julia
Index of a pattern in a String vector with missing values in Julia

Time:12-13

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:

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

As these are the indexes in which the pattern appears.

CodePudding user response:

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.

  • Related