Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by Roland for How to convert an atomic vector to a class of another vector

This looks like an xy problem, but the following function should work in many cases:

convertClass <- function(from, to){  stopifnot(is.atomic(c(unclass(to))))  if (is.factor(to)) return(factor(from))  if (inherits(to, "POSIXct")) return(as.POSIXct(from))  tryCatch(as(from, class(to)), error = function(e) {class(from) <- class(to); from})}

It fails in your first test case but so does as.logical.


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>