tbl_time objects have a time index that contains information about which column should be used for time-based subsetting and other time-based manipulation. Otherwise, they function as normal tibbles.

as_tbl_time(x, index = NULL, ...)

tbl_time(x, index = NULL)

Arguments

x

An object to be converted to tbl_time. This is generally a tibble::tibble(), or an object that can first be coerced to a tibble.

index

The bare column name of the column to be used as the index.

...

Arguments passed to tibble::as_tibble() if coercion is necessary first.

Details

The information stored about tbl_time objects are the index_quo and the index_time_zone. These are stored as attributes, with the index_quo as a rlang::quosure() and the time_zone as a string.

Currently, Date and POSIXct classes are fully supported. yearmon, yearqtr, and hms have experimental support. Due to dplyr's handling of S3 classes like these 3, the classes are lost when you manipulate the index columns directly.

Examples


# Converting a data.frame to a `tbl_time`
# Using Date index
ex1 <- data.frame(date = Sys.Date(), value = 1)
ex1_tbl_time <- as_tbl_time(ex1, date)
class(ex1_tbl_time)
#> [1] "tbl_time"   "tbl_df"     "tbl"        "data.frame"
attributes(ex1_tbl_time)
#> $class
#> [1] "tbl_time"   "tbl_df"     "tbl"        "data.frame"
#> 
#> $row.names
#> [1] 1
#> 
#> $names
#> [1] "date"  "value"
#> 
#> $index_quo
#> <quosure>
#> expr: ^date
#> env:  0x55d1effc0810
#> 
#> $index_time_zone
#> [1] "UTC"
#> 

# Converting a tibble to a `tbl_time`
# Using POSIXct index
ex2 <- tibble::tibble(
  time  = as.POSIXct(c("2017-01-01 10:12:01", "2017-01-02 12:12:01")),
  value = c(1, 2)
)
as_tbl_time(ex2, time)
#> # A time tibble: 2 × 2
#> # Index:         time
#>   time                value
#>   <dttm>              <dbl>
#> 1 2017-01-01 10:12:01     1
#> 2 2017-01-02 12:12:01     2