使用年份扩展时间序列中的日期
Expand date in time series using years
我有以下时间序列
location date value
North 199001 a
North 199203 b
North 199402 c
North 199506 d
South 198005 e
South 198304 f
我想提取年份并扩展每组的行以获得类似
的内容
location date value
North 1990 a
North 1991 a
North 1992 b
North 1993 b
North 1994 c
North 1995 d
South 1980 e
South 1981 e
South 1982 e
South 1983 f
请注意,我想为不在原始数据集中的扩展行重复一个值。我一直在尝试使用 lubridate 和 dplyr,但我做不到。有人可以帮我解决这个问题吗?
A dplyr
/ tidyr
解决方案:substr
将日期列的前四位数字转换为as.numeric
,group_by
位置,complete
每个位置的年份和 fill
值:
代码
library(dplyr)
library(tidyr)
df %>% mutate(date = as.numeric(substr(date, 1, 4))) %>%
group_by(location) %>%
complete(date = full_seq(date, 1)) %>% fill(value)
输出
1 North 1990 a
2 North 1991 a
3 North 1992 b
4 North 1993 b
5 North 1994 c
6 North 1995 d
7 South 1980 e
8 South 1981 e
9 South 1982 e
10 South 1983 f
数据
df <- data.frame(fread("location date value
North 199001 a
North 199203 b
North 199402 c
North 199506 d
South 198005 e
South 198304 f"))
我有以下时间序列
location date value
North 199001 a
North 199203 b
North 199402 c
North 199506 d
South 198005 e
South 198304 f
我想提取年份并扩展每组的行以获得类似
的内容 location date value
North 1990 a
North 1991 a
North 1992 b
North 1993 b
North 1994 c
North 1995 d
South 1980 e
South 1981 e
South 1982 e
South 1983 f
请注意,我想为不在原始数据集中的扩展行重复一个值。我一直在尝试使用 lubridate 和 dplyr,但我做不到。有人可以帮我解决这个问题吗?
A dplyr
/ tidyr
解决方案:substr
将日期列的前四位数字转换为as.numeric
,group_by
位置,complete
每个位置的年份和 fill
值:
代码
library(dplyr)
library(tidyr)
df %>% mutate(date = as.numeric(substr(date, 1, 4))) %>%
group_by(location) %>%
complete(date = full_seq(date, 1)) %>% fill(value)
输出
1 North 1990 a
2 North 1991 a
3 North 1992 b
4 North 1993 b
5 North 1994 c
6 North 1995 d
7 South 1980 e
8 South 1981 e
9 South 1982 e
10 South 1983 f
数据
df <- data.frame(fread("location date value
North 199001 a
North 199203 b
North 199402 c
North 199506 d
South 198005 e
South 198304 f"))