Function to change blanks to NA
This worked for me
df[df == 'NULL'] <- NA
You can directly index fields that match a logical criterion. So you can just write:
df[is_empty(df)] = NA
Where is_empty
is your comparison, e.g. df == ""
:
df[df == ""] = NA
But note that is.null(df)
won’t work, and would be weird anyway1. I would advise against merging the logic for columns of different types, though! Instead, handle them separately.
1 You’ll almost never encounter NULL
inside a table since that only works if the underlying vector is a list
. You can create matrices and data.frames with this constraint, but then is.null(df)
will never be TRUE
because the NULL
values are wrapped inside the list).