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

Issue with gather and time intervals #333

Closed
urskalbitzer opened this issue Jul 26, 2017 · 2 comments
Closed

Issue with gather and time intervals #333

urskalbitzer opened this issue Jul 26, 2017 · 2 comments
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@urskalbitzer
Copy link

urskalbitzer commented Jul 26, 2017

I am trying to use gather with a data frame that contains one column with intervals created with lubridate::interval.

Sys.setenv(TZ = "Canada/Eastern")
library(lubridate, warn.conflicts = FALSE)
library(tidyr)
library(dplyr, warn.conflicts = FALSE)

df <- tibble(period = interval(as.Date("2010-01-01"), as.Date("2010-02-01")),
             var1 = 1,
             var2 = 2)

gather(df, var_name, var_value, var1, var2)
#> # A tibble: 2 x 3
#>                           period var_name var_value
#>                   <S4: Interval>    <chr>     <dbl>
#> 1 2010-01-01 UTC--2010-02-01 UTC     var1         1
#> 2                         NA--NA     var2         2
@hadley hadley added the reprex needs a minimal reproducible example label Nov 15, 2017
@tidyverse tidyverse deleted a comment from urskalbitzer Nov 15, 2017
@tidyverse tidyverse deleted a comment from urskalbitzer Nov 15, 2017
@hadley hadley added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" and removed reprex needs a minimal reproducible example labels Nov 15, 2017
@hadley
Copy link
Member

hadley commented Nov 17, 2017

Slightly smaller reprex:

library(lubridate, warn.conflicts = FALSE)
library(tidyr)

df <- tibble::tibble(
  y = interval(ymd("2010-01-01"), ymd("2010-02-01")),
  x1 = 1,
  x2 = 2
)

gather(df, "key", "value", -y)
#> # A tibble: 2 x 3
#>                                y   key value
#>                   <S4: Interval> <chr> <dbl>
#> 1 2010-01-01 UTC--2010-02-01 UTC    x1     1
#> 2                         NA--NA    x2     2

Created on 2017-11-17 by the reprex package
(0.1.1.9000)
.

@hadley
Copy link
Member

hadley commented Mar 3, 2019

This is resolved in the new pivot() function:

library(lubridate, warn.conflicts = FALSE)
library(tidyr)

df <- tibble::tibble(
  y = interval(ymd("2010-01-01"), ymd("2010-02-01")),
  x1 = 1,
  x2 = 2
)

df %>% pivot(pivot_spec_long(df, -y))
#> # A tibble: 2 x 3
#>   y                              variable value
#>   <S4: Interval>                 <chr>    <dbl>
#> 1 2010-01-01 UTC--2010-02-01 UTC x1           1
#> 2 2010-01-01 UTC--2010-02-01 UTC x2           2

Created on 2019-03-03 by the reprex package (v0.2.1.9000)

@hadley hadley closed this as completed Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

No branches or pull requests

2 participants