r-lib / clock

A Date-Time Library for R
https://clock.r-lib.org
Other
97 stars 4 forks source link

data.table with naive dates creates lists #315

Closed clairerichardsRN closed 1 year ago

clairerichardsRN commented 1 year ago

When I convert a data frame of naive dates (start and end) to a data.table, it turns the dates into lists with one item in each list. Why?

DavisVaughan commented 1 year ago

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page.

You can install reprex by running (you may already have it, though, if you have the tidyverse package installed):

install.packages("reprex")

Thanks

clairerichardsRN commented 1 year ago

reprex is unfortunately giving me an error. All the code isoverlap2<-data.table(overlap) dataframe structure:

structure(list(CountyName = c("King", "King", "King"), OutageID = c(67980, 
67981, 67982), date_start = structure(list(ticks = c(18937L, 
18938L, 18938L), ticks_of_day = c(0L, 9L, 22L)), class = c("clock_naive_time", 
"clock_time_point", "clock_rcrd", "vctrs_rcrd", "vctrs_vctr"), clock = 1L, precision = 5L), 
    date_end = structure(list(ticks = c(18937L, 18938L, 18938L
    ), ticks_of_day = c(21L, 20L, 23L)), class = c("clock_naive_time", 
    "clock_time_point", "clock_rcrd", "vctrs_rcrd", "vctrs_vctr"
    ), clock = 1L, precision = 5L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L), groups = structure(list(
    CountyName = "King", .rows = structure(list(1:3), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))

Then the structure for the data table:

dput(overlap2)


structure(list(CountyName = c("King", "King", "King"), OutageID = c(67980, 
67981, 67982), date_start = structure(list(c(18937L, 18938L, 
18938L), c(0L, 9L, 22L)), clock = 1L, precision = 5L, class = c("clock_naive_time", 
"clock_time_point", "clock_rcrd", "vctrs_rcrd", "vctrs_vctr")), 
date_end = structure(list(c(18937L, 18938L, 18938L), c(21L, 
20L, 23L)), clock = 1L, precision = 5L, class = c("clock_naive_time", 
"clock_time_point", "clock_rcrd", "vctrs_rcrd", "vctrs_vctr"
))), row.names = c(NA, -3L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x000001f3bd37d170>)
This seems to be a similar error reported elsewhere related to POSIXlt vs POSIXct

head(overlap2) Error in [.data.table(x, i, , ) : Column 3 ['date_start'] is length 2 but column 1 is length 3; malformed data.table.


Very old post on [Stack Overflow](https://stackoverflow.com/questions/19355527/r-data-table-set-key-column-length-error)
DavisVaughan commented 1 year ago

Ah, data.table unfortunately doesn't support the rcrd type that clock (and a few other tidyverse packages) build on. There is nothing that I can do about that, sadly. https://clock.r-lib.org/articles/faq.html#why-doesnt-this-work-with-data-table