Effective use of as.numeric () and factor ()

I have several hundred characters imported into R from a database - each has a length of 6-7 million. This is either numerical or factorial data that has a symbol (letters) for labels - with established levels, all factors, all have some NS. As an example

vecA <- c("1",NA, "2",....,NA, "100")
vecB <- c("smith", NA, NA, ... , "jones")

Is there an effective way to force vecA to numeric and vecB to coefficient. The problem is that I don’t know where the numerical and factorial vectors are in the data, and they are tedious to go through them one by one.

+5
source share
2 answers

tryCatch(), "numeric". as.numeric() ( , ), "factor".

vecA <- c("1",NA, "2",NA, "100")
vecB <- c("smith", NA, NA, "jones")

myConverter <- function(X) tryCatch(as.numeric(X), 
                                    warning = function(w) as.factor(X))

myConverter(vecA)
# [1]   1  NA   2  NA 100
myConverter(vecB)
# [1] smith <NA>  <NA>  jones
# Levels: jones smith
+7

, ? , .

convert.numeric <- function(vec) {
  if( grepl("^[0-9]*(\\.[0-9]+)?$",vec)) == !is.na(vec)) ) {
    vec <- as.numeric(vec)
  } else { vec <- as.factor(vec) }
  return(vec)
}

lapply:

new.vectors <- lapply(old.vectors,convert.numeric)
+1

All Articles