Masking Missing Values
Sometimes the code for a missing value gets quite complex (maybe including "and"
or "or" clauses) and difficult to manage. Since you have to specify exactly the
same values in vectors or matrices you can easily make mistakes. One approach is
to cut-and-paste the missing value clause into everything, but that's messy.
Here's an example using a mask.
mask <- !is.na(spc.plt) & !is.na(elev)
slope[mask]
So, we just create a logical variable called mask (actually you can call it
anything you want) and then use it as a logical subscript in any vector or
matrix where we need to eliminate items where something is missing. If you
have lots of missing values in your data, you can have multiple masks and name
them for what they are missing, e.g.
has.elev <- !is.na(elev)