| 1 |
numericIfPossible <- function(x){ |
| 2 |
if(is.atomic(x)) return(.numericIfPossible(x)) |
| 3 |
else { |
| 4 |
res <- lapply(x,.numericIfPossible) |
| 5 |
attributes(res) <- attributes(x) |
| 6 |
return(res) |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
.numericIfPossible <- function(x){ |
| 11 |
if(is.numeric(x)) return(x) |
| 12 |
else if(is.character(x)) return(.Call("numeric_if_possible", x)) |
| 13 |
else if(is.factor(x)) { |
| 14 |
levels <- .Call("numeric_if_possible",levels(x)) |
| 15 |
if(is.numeric(levels)){ |
| 16 |
return(levels[as.numeric(x)]) |
| 17 |
} else return(x) |
| 18 |
} |
| 19 |
else return(x) |
| 20 |
} |