Overview
After reading why does unlist() kill dates in R and the documentation of unlist()
, you've got to manually prevent purrr::map()
from coercing the Date objects to integer by way of using the base::c()
function.
Load mikmart's PR version of purrr::pmap()
After reading pmap strips Date
, it looks like someone very awesome submitted a pull request to resolve this issue within a refactored version of the indexing that happens under the hood in purrr::pmap()
.
Using devtools::dev_mode()
, you can install mikmart/purrr
's "pmap" branch version of purrr
to retain Date objects while using pmap()
.
# ******pmap() example ****
# load necessary packages -----
library(devtools)
library(lubridate)
# enter dev mode so you don't have to uninstall the cran version of purrr ----
dev_mode(on = TRUE)
# install mikmart's PR to fix the coercing of Dates to integer ----
install_github(repo = "mikmart/purrr", ref = "pmap")
# load mikmart's PR version of purrr ----
library(purrr)
# load necessary data
x <- data.frame(d1 = lubridate::ymd(c("2018-01-01","2018-02-01")))
# for the first column in x ----
# give me each element
# note: no need for c()
list.of.dates <-
x %>%
pmap(.f = ~ .x)
# view results -----
list.of.dates
# [[1]]
# [1] "2018-01-01"
#
# [[2]]
# [1] "2018-02-01"
# view the class of each list -----
map_chr(list.of.dates, class) # [1] "Date" "Date"
#
#
# turn off dev mode ---
dev_mode(on = FALSE)
#
# restart R -----
# Manually hit Shift+Cmd+F10 or point in click under the "Session" tab
#
# clear global environment ----
rm(list = ls())
#
# ******map() example********
# load necessary packages -----
library(tidyverse)
library(lubridate)
# load necessary data ----
x <- data.frame(d1 = lubridate::ymd(c("2018-01-01","2018-02-01")))
# from the first column ------
# give me each element
# and ensure the dates don't get coerced to integers
list.of.dates <-
x$d1 %>%
map(.f = ~ .x %>% c())
# view results -----
list.of.dates
# [[1]]
# [1] "2018-01-01"
#
# [[2]]
# [1] "2018-02-01"
# # view the class of each list -----
map_chr(list.of.dates, class) # [1] "Date" "Date"
# end of script #