R の時系列データクラス

Rで学ぶ時系列データ操作

Harrison Brown

Graduate Researcher in Geography

日付・日時クラス

  • numeric

    • 整数、小数など
    • 2022年8月9日 = 19223
    • 1970-01-01 からの経過日数
  • character

    • 文字列、名前など
    • 2022年8月9日 = "2022-08-09"
    • 2022年8月9日 = "August 9, 2022"
Rで学ぶ時系列データ操作

日付・日時クラス

  • Date
    • 日付、年内の日
    • 2022年8月9日 = "2022-08-09"
start_date <- as_date("2022-08-01")
end_date <- as_date("2022-08-09")

end_date - start_date
Time difference of 8 days
  • POSIXct
    • 日付と時刻、タイムゾーン
    • 2022年8月9日 4:17 p.m. =
      • "2022-08-09 20:17:00 UTC"
start_time <-
  as.POSIXct("2022-08-01 08:57:00 EDT")
end_time <-
  as.POSIXct("2022-08-09 15:30:00 EDT")
end_time - start_time
Time difference of 8.272917 days
Rで学ぶ時系列データ操作

Lubridate

lubridate::as_date()

my_date <- as_date("2022-01-20")

my_date
[1] "2022-01-20"
class(my_date)
[1] "Date"

as_date() と base::as.Date() の比較

  • タイムゾーンの扱いが良い
  • 不正な日付形式を警告
  • numeric からの変換が容易
  • ...
Rで学ぶ時系列データ操作

データクラスの判定

class("2022-08-01")
[1] "character"
is.numeric(10494)
[1] TRUE
is.character("April 01, 2003")
[1] TRUE
is.Date(lubridate::today())
[1] TRUE
is.POSIXct(lubridate::now())
[1] TRUE
Rで学ぶ時系列データ操作

演習に進みましょう

Rで学ぶ時系列データ操作

Preparing Video For Download...