將日期時間對象編碼到循環坐標係中,其中由相同持續時間分隔的兩對日期之間的距離相同。
用法
cyclic_encoding(
x,
periods,
encoders = c("sin", "cos"),
week_start = getOption("lubridate.week.start", 7)
)
參數
- x
-
日期時間對象
- periods
-
周期的特征向量。遵循與 period 和 floor_date 函數相同的規範。
- encoders
-
產生編碼的函數名稱。默認為"sin"和"cos"。允許使用任何接受數字輸入的預定義函數的名稱。
- week_start
-
周開始日(默認為 7,周日。設置
lubridate.week.start
進行覆蓋)。一周中各天的完整或縮寫名稱可以是英文或由當前區域設置提供。
例子
times <- ymd_hms("2019-01-01 00:00:00") + hours(0:23)
cyclic_encoding(times, c("day", "week", "month"))
#> sin.day cos.day sin.week cos.week sin.month
#> [1,] 0.000000e+00 1.000000e+00 0.9749279 -0.2225209 0.000000000
#> [2,] 2.588190e-01 9.659258e-01 0.9659258 -0.2588190 0.008445041
#> [3,] 5.000000e-01 8.660254e-01 0.9555728 -0.2947552 0.016889480
#> [4,] 7.071068e-01 7.071068e-01 0.9438833 -0.3302791 0.025332714
#> [5,] 8.660254e-01 5.000000e-01 0.9308737 -0.3653410 0.033774142
#> [6,] 9.659258e-01 2.588190e-01 0.9165623 -0.3998920 0.042213161
#> [7,] 1.000000e+00 -1.608123e-16 0.9009689 -0.4338837 0.050649169
#> [8,] 9.659258e-01 -2.588190e-01 0.8841154 -0.4672686 0.059081565
#> [9,] 8.660254e-01 -5.000000e-01 0.8660254 -0.5000000 0.067509747
#> [10,] 7.071068e-01 -7.071068e-01 0.8467242 -0.5320321 0.075933114
#> [11,] 5.000000e-01 -8.660254e-01 0.8262388 -0.5633201 0.084351066
#> [12,] 2.588190e-01 -9.659258e-01 0.8045978 -0.5938202 0.092763002
#> [13,] -3.216245e-16 -1.000000e+00 0.7818315 -0.6234898 0.101168322
#> [14,] -2.588190e-01 -9.659258e-01 0.7579717 -0.6522874 0.109566427
#> [15,] -5.000000e-01 -8.660254e-01 0.7330519 -0.6801727 0.117956717
#> [16,] -7.071068e-01 -7.071068e-01 0.7071068 -0.7071068 0.126338595
#> [17,] -8.660254e-01 -5.000000e-01 0.6801727 -0.7330519 0.134711462
#> [18,] -9.659258e-01 -2.588190e-01 0.6522874 -0.7579717 0.143074722
#> [19,] -1.000000e+00 -1.836970e-16 0.6234898 -0.7818315 0.151427778
#> [20,] -9.659258e-01 2.588190e-01 0.5938202 -0.8045978 0.159770033
#> [21,] -8.660254e-01 5.000000e-01 0.5633201 -0.8262388 0.168100894
#> [22,] -7.071068e-01 7.071068e-01 0.5320321 -0.8467242 0.176419766
#> [23,] -5.000000e-01 8.660254e-01 0.5000000 -0.8660254 0.184726056
#> [24,] -2.588190e-01 9.659258e-01 0.4672686 -0.8841154 0.193019171
#> cos.month
#> [1,] 1.0000000
#> [2,] 0.9999643
#> [3,] 0.9998574
#> [4,] 0.9996791
#> [5,] 0.9994295
#> [6,] 0.9991086
#> [7,] 0.9987165
#> [8,] 0.9982532
#> [9,] 0.9977186
#> [10,] 0.9971129
#> [11,] 0.9964361
#> [12,] 0.9956882
#> [13,] 0.9948693
#> [14,] 0.9939795
#> [15,] 0.9930187
#> [16,] 0.9919872
#> [17,] 0.9908849
#> [18,] 0.9897119
#> [19,] 0.9884683
#> [20,] 0.9871543
#> [21,] 0.9857698
#> [22,] 0.9843150
#> [23,] 0.9827901
#> [24,] 0.9811950
plot(cyclic_encoding(times, "1d"))
plot(cyclic_encoding(times, "2d"), xlim = c(-1, 1))
plot(cyclic_encoding(times, "4d"), xlim = c(-1, 1))
相關用法
- R lubridate DateTimeUpdate 更改日期對象的組成部分
- R lubridate stamp 基於人性化模板設置日期和時間格式
- R lubridate interval 用於創建和操作 Interval 對象的實用程序
- R lubridate is.difftime x 是 difftime 對象嗎?
- R lubridate as_date 將對象轉換為日期或日期時間
- R lubridate date 獲取/設置日期時間的日期部分
- R lubridate round_date 日期時間對象的舍入、取整和取整方法
- R lubridate make_difftime 創建一個 difftime 對象。
- R lubridate is.timespan x 是時間長度嗎?
- R lubridate with_tz 獲取不同時區的日期時間
- R lubridate mplus 在日期中添加和減去月份,但不超過新月份的最後一天
- R lubridate as.interval 將對象更改為間隔
- R lubridate second 獲取/設置日期時間的秒部分
- R lubridate quarter 獲取日期時間的財政季度和學期
- R lubridate posix_utils 各種 POSIX 實用程序
- R lubridate date_decimal 將小數轉換為日期
- R lubridate as.duration 將對象更改為持續時間
- R lubridate hour 獲取/設置日期時間的小時部分
- R lubridate minute 獲取/設置日期時間的分鍾部分
- R lubridate month 獲取/設置日期時間的月份部分
- R lubridate duration 創建一個持續時間對象。
- R lubridate leap_year 一年是閏年嗎?
- R lubridate local_time 從日期時間向量獲取當地時間。
- R lubridate make_datetime 從數字表示高效創建日期時間
- R lubridate week 獲取/設置日期時間的周組成部分
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Cyclic encoding of date-times。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。