LoginSignup
0
0

More than 1 year has passed since last update.

年月単位の連続データをRで作る

Posted at

仕事でいろいろなデータを集計していると、月別にまとめたいことが多々あります。
しかし月単位で連続のデータを生成する関数がないので方法を考えました

.R
> library(tidyverse)
> library(lubridate)

#2020年1月から2022年5月まで、連続の月次データを生成する
> year_month <- seq(as.Date("2020-01-01"), as.Date("2022-05-01"),by="1 day") %>% floor_date(unit="months") %>% unique()
# [1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01" "2020-11-01" "2020-12-01" "2021-01-01"
#[14] "2021-02-01" "2021-03-01" "2021-04-01" "2021-05-01" "2021-06-01" "2021-07-01" "2021-08-01" "2021-09-01" "2021-10-01" "2021-11-01" "2021-12-01" "2022-01-01" "2022-02-01"
#[27] "2022-03-01" "2022-04-01" "2022-05-01"

> class(year_month)
[1] "Date"

月次データの完成です。

0
0
2

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
0
0