Skip to contents

This function transforms a series of time or datetime to time indexes.

Usage

transform_time_id(obsTime, timeUnit, timeStep)

Arguments

obsTime

Date/Datetime/Numeric. A vector of observed time of hot spots. If timeUnit is "n", obsTime needs to be a numeric vector, otherwise, it needs to be in date or datetime format.

timeUnit

Character. The unit of time, one of "s" (seconds), "m"(minutes), "h"(hours), "d"(days) and "n"(numeric).

timeStep

Numeric (>0). Number of units of timeUnit in a time step.

Value

Integer. A vector of time indexes.

Details

The earliest time is assigned with a time index 1. The difference between any other time to the earliest time is transformed using the timeUnit and divided by the timeStep. These differences are floored to integer and used as the time indexes.

Examples


# Define obsTime
obsTime <- as.Date(c("2020-01-01",
                     "2020-01-02",
                     "2020-01-04"))

# Transform it to time index under different settings
transform_time_id(obsTime, "h", 1)
#> 
#> ── "1" time index = 1 hour 
#>  Transform observed timetime indexes
#>  73 time indexes found
#> [1]  1 25 73
transform_time_id(obsTime, "m", 60)
#> 
#> ── "1" time index = 60 mins 
#>  Transform observed timetime indexes
#>  73 time indexes found
#> [1]  1 25 73
transform_time_id(obsTime, "s", 3600)
#> 
#> ── "1" time index = 3600 secs 
#>  Transform observed timetime indexes
#>  73 time indexes found
#> [1]  1 25 73

# Define numeric obsTime
obsTime <- c(1,
             1.5,
             4.5,
             6)

# Transform it to time index under different settings
transform_time_id(obsTime, "n", 1)
#> 
#> ── "1" time index = 1 numeric 
#>  Transform observed timetime indexes
#>  6 time indexes found
#> [1] 1 1 4 6
transform_time_id(obsTime, "n", 1.5)
#> 
#> ── "1" time index = 1.5 numeric 
#>  Transform observed timetime indexes
#>  4 time indexes found
#> [1] 1 1 3 4