Search code examples
rdplyrtibble

How to initialize tibble with difftime column


I am getting error

Error in as.POSIXct(time1) : argument "time1" is missing, with no default

when I try to initialize tibble column with difftime class:

tibble(Time_diff = difftime())

Should I treat it as character and convert it to difftime once there're some data?


Solution

  • How about

    tibble(Time_diff=structure(NA_real_, class = "difftime"))
    

    which gives

    # A tibble: 1 x 1
      Time_diff
      <drtn>   
    1 NA       
    

    or

    tibble(Time_diff=structure(NA_real_, class = "difftime")) %>% filter(FALSE)
    

    which gives

    # A tibble: 0 x 1
    # … with 1 variable: Time_diff <drtn>
    

    if you want an empty tibble containing a column of the required class?