R拾取经济衰退期的开始日期和结束日期
问题描述
我正在尝试在 R 中绘制衰退阴影期.考虑以下示例,衰退期被识别为 1,非衰退期被识别为 0.
日期衰退1918-09-01 11918-10-01 11918-11-01 11918-12-01 11919-01-01 11919-02-01 11919-03-01 11919-04-01 01919-05-01 01919-06-01 01919-07-01 01919-08-01 01919-09-01 01919-10-01 01919-11-01 01919-12-01 01920-01-01 01920-02-01 11920-03-01 11920-04-01 11920-05-01 1
谁能帮我确定经济衰退期的开始日期和结束日期?例如:
开始结束1918-09-01 1919-03-011920-02-01 1920-05-01
几年前也有人问过同样的问题,但我认为答案无法解决这个问题.请参阅 R 衰退日期转换
提前致谢!
解决方案使用 data.table
包中的 rleid()
函数:
库(data.table)data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)][衰退 == 1,.(开始 = V1,结束 = V2)]
<块引用>
开始 结束1: 1918-09-01 1919-03-012: 1920-02-01 1920-05-01
说明
第一个data.table
表达式查找所有 个周期的开始和结束日期.rleid()
是一个方便的函数,用于生成用于分组操作的游程类型 id 列.
data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)]
<块引用>
rleid Recession V1 V21:1 1 1918-09-01 1919-03-012:2 0 1919-04-01 1920-01-013:3 1 1920-02-01 1920-05-01
第二个表达式仅选择衰退期并返回 Start
和 End
日期.
数据
DF <- readr::read_table(日期衰退1918-09-01 11918-10-01 11918-11-01 11918-12-01 11919-01-01 11919-02-01 11919-03-01 11919-04-01 01919-05-01 01919-06-01 01919-07-01 01919-08-01 01919-09-01 01919-10-01 01919-11-01 01919-12-01 01920-01-01 01920-02-01 11920-03-01 11920-04-01 11920-05-01 1")
I'm trying to plot the Recession Shading periods in R. Consider the following example, recession periods are recognized as 1, and non recession periods are 0.
Date Recession
1918-09-01 1
1918-10-01 1
1918-11-01 1
1918-12-01 1
1919-01-01 1
1919-02-01 1
1919-03-01 1
1919-04-01 0
1919-05-01 0
1919-06-01 0
1919-07-01 0
1919-08-01 0
1919-09-01 0
1919-10-01 0
1919-11-01 0
1919-12-01 0
1920-01-01 0
1920-02-01 1
1920-03-01 1
1920-04-01 1
1920-05-01 1
Can anyone help me to pick up the starting date and ending dates of the recession periods? For example:
Start End
1918-09-01 1919-03-01
1920-02-01 1920-05-01
The same question has been asked few years ago, but I think the answer is not able solve this question. see R Recession Dates Conversion
Thanks in advance!
解决方案Using the rleid()
function from the data.table
package:
library(data.table)
data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)][
Recession == 1, .(Start = V1, End = V2)]
Start End 1: 1918-09-01 1919-03-01 2: 1920-02-01 1920-05-01
Explanation
The first data.table
expression finds start and end dates of all periods. rleid()
is a convenience function for generating a run-length type id column to be used in grouping operations.
data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)]
rleid Recession V1 V2 1: 1 1 1918-09-01 1919-03-01 2: 2 0 1919-04-01 1920-01-01 3: 3 1 1920-02-01 1920-05-01
The second expression picks only the Recession periods and returns the Start
and End
dates.
Data
DF <- readr::read_table(
"Date Recession
1918-09-01 1
1918-10-01 1
1918-11-01 1
1918-12-01 1
1919-01-01 1
1919-02-01 1
1919-03-01 1
1919-04-01 0
1919-05-01 0
1919-06-01 0
1919-07-01 0
1919-08-01 0
1919-09-01 0
1919-10-01 0
1919-11-01 0
1919-12-01 0
1920-01-01 0
1920-02-01 1
1920-03-01 1
1920-04-01 1
1920-05-01 1 "
)
相关文章