Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pivot_wider not working with class <Period> #916

Closed
bertrandh opened this issue Mar 20, 2020 · 1 comment
Closed

pivot_wider not working with class <Period> #916

bertrandh opened this issue Mar 20, 2020 · 1 comment

Comments

@bertrandh
Copy link

bertrandh commented Mar 20, 2020

Brief description of the problem:
pivot_wider throws a strange error when used with a value_from column of type <Period> (as output from lubridate).
I am using R-3.6.3, lubridate_1.7.4, dbplyr_1.4.2.

Reprex:

df_0 <- tibble(period_name   = c("a", "b", "c"), 
               period_value  = c("1:1:1", 
                                 "2:2:2", 
                                 "4:4:4")) %>% 
         mutate(period_value = lubridate::hms(period_value))

df_0
## A tibble: 3 x 2
#  period_name period_value
#  <chr>       <Period>
#1 a           1H 1M 1S
#2 b           2H 2M 2S
#3 c           4H 4M 4S

Now, let's try widening the data frame

df_0 %>% pivot_wider(names_from = period_name, values_from = period_value)
#Error: No common type for `value` <Period> and `x` <Period>.

This error does not make sense to me.
BTW, at least it throws an error, whereas spread silently messed it up (see Stackoverflow question).

It works fine, though, with date_time type:

df_1 <- tibble(dttm_name   = c("a", "b", "c"), 
               dttm_value  = c("2020-01-01 1:1:1", 
                               "2020-02-03 2:2:2", 
                               "2020-05-10 4:4:4")) %>% 
         mutate(dttm_value = lubridate::as_datetime(dttm_value))

df_1
## A tibble: 3 x 2
#  dttm_name dttm_value
#  <chr>     <dttm>
#1 a         2020-01-01 01:01:01
#2 b         2020-02-03 02:02:02
#3 c         2020-05-10 04:04:04

df_1 %>% pivot_wider(names_from = dttm_name, values_from = dttm_value)
## A tibble: 1 x 3
#  a                   b                   c
#  <dttm>              <dttm>              <dttm>
#1 2020-01-01 01:01:01 2020-02-03 02:02:02 2020-05-10 04:04:04

Is that something wrong with pivot_wider (and spread) or with lubridate's Period class?

@hadley
Copy link
Member

hadley commented Mar 20, 2020

Duplicate of r-lib/vctrs#919

@hadley hadley marked this as a duplicate of r-lib/vctrs#919 Mar 20, 2020
@hadley hadley closed this as completed Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants