Why does unlist() kill dates in R?

Or using purrr to flatten a list of dates to a vector preserving types:

list(as.Date(c("2013-01-01", "2013-02-01", "2013-03-01"))) %>% purrr::reduce(c)

results in

[1] "2013-01-01" "2013-02-01" "2013-03-01"

do.call is a handy function to "do something" with a list. In our case, concatenate it using c. It's not uncommon to cbind or rbind data.frames from a list into a single big data.frame.

What we're doing here is actually concatenating elements of the dd list. This would be analogous to c(dd[[1]], dd[[2]]). Note that c can be supplied as a function or as a character.

> dd <- list(dd, dd)
> (d <- do.call("c", dd))
[1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-01-01" "2013-02-01" "2013-03-01"
> class(d) # proof that class is still Date
[1] "Date"

Tags:

R