SubString and missing

with this vector
str = [“az”, “by”, missing, “cx”]

how can I get this returned using SubString?
[“a”, “b”, missing, “c”]

Thank you!

For example:

julia> str = ["az", "by", missing, "cx"];

julia> [ ismissing(x) ? missing : SubString(x,1,1) for x in str ]
4-element Vector{Union{Missing, SubString{String}}}:
 "a"
 "b"
 missing
 "c"

3 Likes

Not sure if you actually need a substring, but if you can use character instead, try replacing SubString with first.

Missings.jl has the function passmissing, so you would write

julia> str = ["az", "by", missing, "cx"];

julia> [passmissing(SubString)(x, 1, 1) for x in str]
4-element Vector{Union{Missing, SubString{String}}}:
 "a"
 "b"
 missing
 "c"
1 Like