R apply functional that keeps attributes

Viewed 310

How can I keep the attributes (class etc.) of an object after applying a vector functional from R, such as sapply or vapply?

> d <- c(as.POSIXct(Sys.time()), as.POSIXct(Sys.time()) + 60)
> typeof(d)
[1] "double"
> class(d)
[1] "POSIXct" "POSIXt"

> sapply(d, function(x) x)
[1] 1510760756 1510760816
> vapply(d, function(x) x, double(1))
[1] 1510760756 1510760816

I could circumvent this problem with

> do.call(c, as.list(d))
[1] "2017-11-15 16:45:56 CET" "2017-11-15 16:46:56 CET"

but then I have to build a list first.

Thank you!

1 Answers
Related