You might need to do some checking. You cannot safely convert factors directly to numeric. as.character
must be applied first. Otherwise, the factors will be converted to their numeric storage values. I would check each column with is.factor
then coerce to numeric as necessary.
df1[] <- lapply(df1, function(x) {
if(is.factor(x)) as.numeric(as.character(x)) else x
})
sapply(df1, class)
# a b
# "numeric" "numeric"