12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

エクセルの日時変数をRで扱う

Last updated at Posted at 2015-06-28

エクセルなどの表計算ソフトでは、日付や時間は数値で表されている。
このことは、日付を打ち込んでからセルを数値に変換すると、3~4万くらいの数字になることで確認できる。これは実は、1889年12月30日からの経過日数になっているらしい。
なんで1900年1月1日じゃないかというと、歴史的経緯にがあるとのこと。

そういうわけでエクセルのファイルをRで読み込むと、日付・時間変数が数値で与えられることがある。
そういう場合は、以下のコードで日付・時間型に変換できる。

# 06/29/15,  05/15/15 03:20 PM
numeric.date <- c(42184.00000, 42139.63895)

# 日時
as.POSIXct('1899-12-30') + as.difftime(numeric.date, units = 'days')
# [1] "2015-06-29 00:00:00 JST" "2015-05-15 15:20:05 JST"

# 日付のみ
as.Date(numeric.date, origin = "1899-12-30")
# [1] "2015-06-29" "2015-05-15"

# これはズレるので注意
as.Date('1899-12-30') + as.difftime(numeric.date, units = 'days')
# [1] "2015-06-29" "2015-05-16"

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?