使用年来扩大时间序列的日期
我有以下时间序列,
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 但我无法做到这一点。有人能帮我解决这个问题吗?
I have the following time series
location date value
North 199001 a
North 199203 b
North 199402 c
North 199506 d
South 198005 e
South 198304 f
I would like to extract the years and expand the rows per group to obtain something like
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
Note that I would like to repeat a value for the rows expanded that were not in the original dataset. I have been trying using lubridate and dplyr but I'm not being able to do it. Can anybody help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dplyr
/tidyr
解决方案:substr
充当日期列的前四位数字,转换为as.numeric
,group_by
位置,完整
每个位置的年份并填写
值:代码
输出
数据
A
dplyr
/tidyr
solution:substr
act the first four digits of your date column, convert toas.numeric
,group_by
location,complete
the years per location andfill
the values:Code
Output
Data