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

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

$
0
0

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 all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images

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