I have several hundred character vectors imported into R from a database – each has length of 6-7 million. They are either numeric or factor data that has character(letters) for labels – with levels to be set,all factor, all have some NAs. As an example
vecA <- c("1",NA, "2",....,NA, "100")
vecB <- c("smith", NA, NA, ... , "jones")
Is there an efficient way to coerce vecA to numeric and vecB to factor. The problem is I don’t know where the numeric and factor vectors are in the data and it’s tedious to go through them one by one.
I’d probably use
tryCatch(), attempting first to convert each vector to class"numeric". Ifas.numeric()throws a warning message (as it will when the input vector contains non-numeric characters), I’d catch the warning and instead convert the vector to class"factor".