I have access to a data set saved as an .Rdata file that I need to clean up. All the columns are stored as character but some of them are actually numeric. And some of these variables that are supposed to be numeric have character strings in them.
I want to convert all columns that are supposed to be numeric to numeric and replace any stray character elements with NA. Here's a simple example of the problem.
library(data.table)
temp <- c("1", "2", "3", "notANumber", "4", "5", "NaN2")
dt <- data.table(a = temp)
dt[, a := as.numeric(a)]
The last line of this code returns a warning that the character elements were forced to NA.
I'd like to do that under my control and get rid of the warning.