I've noticed that when you do this:
mapply(function(x) { x }, c(as.Date('2014-1-1'), as.Date('2014-2-2')))
R automatically converts your vector of Dates into a vector of numbers. Is there a way to disable this behavior?
I know that you can wrap the result in as.Date(..., origin='1970-1-1'), but I can only imagine there has to be a better solution here.
This has to do with the way mapply
simplifies its result through simplify2array
.
x <- list(as.Date('2014-1-1'), as.Date('2014-2-2'))
simplify2array(x, higher = FALSE)
# [1] 16071 16103
You can turn off the simplification and then reduce the list manually.
do.call(c, mapply(I, x, SIMPLIFY = FALSE))
# [1] "2014-01-01" "2014-02-02"
Or you can use Map
along with Reduce
(or do.call
)
Reduce(c, Map(I, x))
# [1] "2014-01-01" "2014-02-02"
Map
is basically mapply(..., SIMPLIFY = FALSE)
and I use I
in place of function(x) { x }
because it just returns its input as-is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With