トップ 履歴 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

時間の処理

*disclaimer
603500

R

時間の処理

as.Date()

  • 文字列ではなくDateという日付クラスのデータとして扱うようにする。as.Date() Dは大文字
  • difftime(A,B)で、何日差があるか出る
    • 「差がある」というのは「何日ずれているか」ということで、今日と明日とでは1日差があると数える
> as.Date("2019-10-25")
[1] "2019-10-25"
> class(as.Date("2019-10-25"))
[1] "Date"
> day1 <- as.Date("2019-10-25")
> day2 <- as.Date("2020-10-25")
> difftime(day1, day2)
Time difference of -366 days
> difftime(day2, day1)
Time difference of 366 days

difftime() のオプション

  • units="単位" で数える単位を指定できる
secs
mins
hours
days
weeks
> difftime(day2, day1)
Time difference of 366 days
> difftime(day2, day1, units="weeks")
Time difference of 52.28571 weeks
> difftime(day2, day1, units="hours")
Time difference of 8784 hours
> difftime(day2, day1, units="mins")
Time difference of 527040 mins
> difftime(day2, day1, units="secs")
Time difference of 31622400 secs

数え方の単位

sec

    0–61: seconds.
min

    0–59: minutes.
hour

    0–23: hours.
mday

    1–31: day of the month
mon

    0–11: months after the first of the year.
year

    years since 1900.
wday

    0–6 day of the week, starting on Sunday.
yday

    0–365: day of the year.

日時の表記法

"2011-03-27 01:30:00", "%Y-%m-%d %H:%M:%S"