This must surely have been asked before, and I apologize if this is a duplicate, but I did not find a previous relevant question.
Suppose I have, as a column in a data.table
read from a file, a vector of strings, with each string containing either a number (e.g. "3.14"), or indicating that no number was observed (e.g. "not observed"). I would like to convert this column to numeric, and replace entries that say "not observed" with a default, such as 10 (n.b. - this is an example, I'd like a solution that also works for any other numeric value).
If I say
# Toy data
data <- data.table(Column = c("3.14", "2.718", "not observed"))
data[, Column := fifelse(Column == "not observed", 10, as.numeric(Column))]
I get the correct result, but I also get a warning saying NAs introduced by coercion
, presumably because as.numeric(Column)
is evaluated for the entire column, including the cells containing the string "not observed", before fifelse()
("fast if/else", from the data.table
package) runs.
The warning's harmless but ugly, and I'd like to get rid of it. I'm no fan of suppressing warnings however, and would like to find an alternate way of doing the same thing. Is there any? I'd ideally like to keep using data.table
syntax, e.g. :=
, since my real data.table
is (much) taller, and execution times matter.
Thank you!