使用年来扩大时间序列的日期

发布于 2025-01-19 12:41:23 字数 696 浏览 3 评论 0原文

我有以下时间序列,

         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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2025-01-26 12:41:23

dplyr / tidyr 解决方案:substr充当日期列的前四位数字,转换为 as.numericgroup_by 位置,完整每个位置的年份并填写值:

代码

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"))

A dplyr / tidyr solution: substract the first four digits of your date column, convert to as.numeric, group_by location, complete the years per location and fill the values:

Code

library(dplyr)
library(tidyr)

df %>% mutate(date = as.numeric(substr(date, 1, 4))) %>% 
  group_by(location) %>% 
  complete(date = full_seq(date, 1)) %>% fill(value)

Output

 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 

Data

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"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文