Quantcast
Channel: How to convert an atomic vector to a class of another vector - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to convert an atomic vector to a class of another vector

$
0
0

I'm trying to write a function that coerces an atomic vector to class of another vector, but it doesn't work very well...

convertClass <- function(from, to){    # this is really ugly and fails in some cases    eval(parse(text = paste0("as.", class(to), "(", deparse(dput(from)), ")")))}convertClass(c("1", "0"), c(FALSE)) # FAILS, desired output is c(TRUE, FALSE)convertClass(c("1", "0"), c(1L))convertClass(c("1", "0"), c(1.5))convertClass(c("1", "0"), c("XYZ"))convertClass(c("1", "0"), factor("A"))

Any more elegant and actually functional solution?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images