2021年5月20日木曜日

Rでの日付操作方法_lubridateの使い方

概要

Rで日付データを編集するにあたり便利なライブラリ「lubridate」を利用する事にしたので、その使い方をメモします。


環境

R:4.0.3


インストール

まずはインストールから。
インストールはRでおなじみのインストールコマンドで可能です。
> install.packages("lubridate")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/kk/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
also installing the dependency ‘generics’
 URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/generics_0.1.0.zip' を試しています 
Content type 'application/zip' length 70676 bytes (69 KB)
downloaded 69 KB
 URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/lubridate_1.7.10.zip' を試しています 
Content type 'application/zip' length 1754612 bytes (1.7 MB)
downloaded 1.7 MB
package ‘generics’ successfully unpacked and MD5 sums checked
package ‘lubridate’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
	C:\Users\kk\AppData\Local\Temp\RtmpO0mSLb\downloaded_packages
>

使い方
使い方は以下のとおりになります。年月日時分秒のうち表示したいものをymdhmsを用いて関数で表現してあげればOKです。
y : year (年)
m : month (月)
d : day(日)
h : hour(時)
m : minutes(分)
s : seconds(秒)

> # ライブラリの読み込み
> library(lubridate)

 次のパッケージを付け加えます: ‘lubridate’ 

 以下のオブジェクトは ‘package:base’ からマスクされています: 

     date, intersect, setdiff, union 

 警告メッセージ: 
 パッケージ ‘lubridate’ はバージョン 4.0.5 の R の下で造られました  
> # 実行方法
> ymd("2021-5-1")
[1] "2021-05-01"

> mdy("1-5-2021")
[1] "2021-05-01"

> hms("11:30:42")
[1] "11H 30M 42S"

> ymd_hms("2021-05-01 11:30:42")
[1] "2021-05-01 11:30:42 UTC"

> ymd_hm("2021-05-01 11:30",tz="Japan")
[1] "2021-05-01 11:30:00 JST"

本日はここまでで。

0 件のコメント:

コメントを投稿

FX自動トレード_2021年6月実績

      概要 2021年6月が終わり、FX自動トレードによる収益実績を取り纏めましたので振り返ってみたいと思います。 2021年6月結果 2021年6月の実績は、実利利益額:\66,761、実績利益率は1.42%でした。 今月もボラが全く出ず。利益率の連続過去最低記録更新は免...