markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

`pivot_wider` returning NA when names_from is a date #759

Closed mattsams89 closed 1 year ago

mattsams89 commented 1 year ago

This is a weird one I only discovered after updating our server's version from 0.8.? to 0.10 over the weekend, so this bug may have appeared earlier. pivot_wider() doesn't seem to like dates very much, but the issue can be circumvented by converting the dates to character first:

library(tidytable)

set.seed(42)
employees <- 
    tidytable(employee = c("Bob", "Cindy", "Murph"), 
              employee_id = 1:3, 
              start_date = Sys.Date() - sample(500:10000, 3))

pivot_wider(employees, c(employee, start_date), employee_id)

# A tidytable: 1 × 3
  Bob_NA Cindy_NA Murph_NA
   <int>    <int>    <int>
1      1        2        3

pivot_wider(employees, start_date, employee_id)

# A tidytable: 3 × 2
  employee  `NA`
  <chr>    <int>
1 Bob          1
2 Cindy        2
3 Murph        3

mutate(employees, start_date = as.character(start_date)) |> 
    pivot_wider(c(employee, start_date), employee_id)

# A tidytable: 1 × 3
  `Bob_2015-06-03` `Cindy_2007-06-21` `Murph_1996-06-21`
             <int>              <int>              <int>
1                1                  2                  3

I assume this is related to some of the typing improvements you've been working on over the last few months, but I've been OOTL on that process so my investigative abilities on where the issue might be coming from are limited.

Aside from dates, I've only tested it with character and numeric. Both seem to work fine, so it may just be an issue with the attributes that are attached to date types. I can do some further investigation, though!

markfairbanks commented 1 year ago

Hmm that's odd I'll take a look.