How remove missing value from vector

How do I remove these missing values:

a = ["NY", "NY", "NY", missing, missing]

You can wrap it in an iterator that skips missing values:

skipmissing(a)

If you need to actually materialize that vector you can collect it:

collect(skipmissing(a))

You could also use filter:

filter(!ismissing, a)  # this copies the data into a new vector
# or
filter!(!ismissing, a)  # this modifies the vector a in-place.
13 Likes

What are the disadvantages of the Matlab-style (?) solution: a[.~ismissing.(a)] ?
Thanks.

It allocates, while skipmissing does not.
Also seems harder to read.

4 Likes

It actually allocates two arrays, first a BitVector for indexing, and then the output vector.

Also, ~ is bitwise not, and while that works well on a BitVector, it seems risky and implementation-dependent. a[.!ismissing.(a)] seems more semantically correct.

Edit: My spellchecker always replaces ismissing with dismissing. Happens all the time, suddenly.

1 Like

@DNF and @hendri54, thank you. It is hard to get rid of bad habits and your explanations definitely help.