purrr icon indicating copy to clipboard operation
purrr copied to clipboard

transpose & flatten declasses (at least `dttm` objects)

Open mmuurr opened this issue 4 years ago • 3 comments

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?

mmuurr avatar Nov 15 '20 06:11 mmuurr

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

mmuurr avatar Nov 15 '20 06:11 mmuurr

This will be fixed once we implement purrr on top of vctrs.

lionel- avatar Nov 16 '20 09:11 lionel-

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.

jonathan-g avatar Jan 16 '21 23:01 jonathan-g

Duplicate of #471, which has moved to #875

hadley avatar Aug 24 '22 09:08 hadley