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?