purrr
purrr copied to clipboard
transpose & flatten declasses (at least `dttm` objects)
Using purrr v0.3.4, when I transpose a data.frame with a datetime column, that column is de-classed:
> x <- tibble(now = Sys.time())
> x
# A tibble: 1 x 1
# now
# <dttm>
# 1 2020-11-14 23:12:35
> transpose(x)
# [[1]]
# [[1]]$now
# [1] 1605420756
Is this expected?
I've just noticed that flatten
does the same thing:
> list(Sys.time())
# [[1]]
# [1] "2020-11-14 23:34:39 MST"
> list(Sys.time()) %>% flatten()
# [[1]]
# [1] 1605422083
This will be fixed once we implement purrr on top of vctrs.
In the meantime, I have found that purrr::pmap(list)
is a workaround for turning a tibble
into a list of rows (i.e., a list of lists where each element of the outer list is a row of the original tibble), while preserving class attributes.
library(purrr)
library(tibble)
foo <- tibble(now = Sys.time())
foo
#> # A tibble: 1 x 1
#> now
#> <dttm>
#> 1 2021-01-16 17:25:02
purrr::transpose(foo)
#> [[1]]
#> [[1]]$now
#> [1] 1610839503
purrr::pmap(foo, list)
#> [[1]]
#> [[1]]$now
#> [1] "2021-01-16 17:25:02 CST"
Created on 2021-01-16 by the reprex package (v0.3.0)
Acknowledgements
@jennybc's fabulous tutorials on data rectangling led me to this solution.
Duplicate of #471, which has moved to #875