简体   繁体   English

汇总日期数据

[英]Summarize data across dates

This is a follow up question to one posted earlier (here) : 这是对较早发布的一个后续问题(此处)

The data looks like: 数据如下:

# A tibble: 54 x 5
   ID       start_date end_date   date                  val
   <chr>    <date>     <date>     <dttm>              <dbl>
 1 00036110 2005-04-30 2006-03-31 2005-04-30 00:00:00  14.7
 2 00036110 2005-04-30 2006-03-31 2006-03-31 00:00:00  28.5
 3 00036110 2006-04-30 2007-03-31 2006-04-30 00:00:00  26.7
 4 00036110 2006-04-30 2007-03-31 2007-03-31 00:00:00  27.6
 5 00036110 2007-04-30 2008-03-31 2007-04-30 00:00:00  30.5
 6 00036110 2007-04-30 2008-03-31 2008-03-31 00:00:00  27.3
 7 00036110 2008-04-30 2009-03-31 2008-04-30 00:00:00  23.4
 8 00036110 2008-04-30 2009-03-31 2009-03-31 00:00:00  12.5
 9 00036110 2008-04-30 2009-03-31 2008-04-30 00:00:00  23.4
10 00036110 2008-04-30 2009-03-31 2009-03-31 00:00:00  12.5
# … with 44 more rows

I can apply the solution suggested to me earlier and use: 我可以将之前建议的解决方案应用于我并使用:

d %>%
  group_by(ID) %>%
  summarise(start_date = start_date[1],
            end_date = end_date[1],
            diff = (val[2] - val[1]) / val[1],
            val_start = val[1],
            val_end = val[2]) %>%
  arrange(start_date, end_date)

Which gives me the following output: 这给了我以下输出:

# A tibble: 2 x 6
  ID       start_date end_date    diff val_start val_end
  <chr>    <date>     <date>     <dbl>     <dbl>   <dbl>
1 00036110 2005-04-30 2006-03-31 0.933      14.7    28.5
2 00036020 2010-04-30 2011-03-31 0.362      24.1    32.9

Ie it only gives me the "first" observation it sees. 也就是说,它只给我它所看到的“第一”观察结果。

I would like to have the data for all year combinations where the start_date = date and end_date = date : Expected output: 我想获得所有年份组合的数据,其中start_date = dateend_date = date :预期输出:

# A tibble: 2 x 6
  ID       start_date end_date    diff val_start val_end
  <chr>    <date>     <date>     <dbl>     <dbl>   <dbl>
1 00036110 2005-04-30 2006-03-31 0.933      14.7    28.5
2 00036110 2006-04-30 2007-03-31 0.034      26.7    27.6
3 00036110 2007-04-30 2008-03-31 -0.104     30.5    27.3
4 00036110 2008-04-30 2009-03-31 -0.466     23.4    12.5
5 00036110 2009-04-30 2010-03-31 -0.466     23.4    12.5

(there are some duplicates - ref lines 4 and 5 above) (有一些重复项-上面的参考线4和5)

Where observation 1 corresponds to lines 1 and 2 of the d data, where date = start_date in line 1 and date = end_date in line 2. Observation 2 corresponds to lines 3 and 4 of the d data, where date corresponds to start_date in line 3 and date corresponds to end_date in line 4. etc. 其中观察值1对应于d数据的第1行和第2行,其中date =第1行中的start_datedate =第2行中的end_date 。观察值2对应于d数据的第3行和第4行,其中date对应于第3行中的start_datedate对应于第4行中的end_date

If anyone can point me in the right direction, that would be great! 如果有人能指出我正确的方向,那就太好了!

Data: 数据:

d <- structure(list(ID = c("00036110", "00036110", "00036110", "00036110", 
"00036110", "00036110", "00036110", "00036110", "00036110", "00036110", 
"00036110", "00036110", "00036110", "00036110", "00036110", "00036110", 
"00036020", "00036020", "00036110", "00036110", "00036020", "00036020", 
"00036110", "00036110", "00036020", "00036020", "00036110", "00036110", 
"00036020", "00036020", "00036110", "00036110", "00036020", "00036020", 
"00036020", "00036020", "00036110", "00036110", "00036020", "00036020", 
"00036020", "00036020", "00036110", "00036110", "00036020", "00036020", 
"00036020", "00036020", "00036110", "00036110", "00036020", "00036020", 
"00036110", "00036020"), start_date = structure(c(12903, 12903, 
13268, 13268, 13633, 13633, 13999, 13999, 13999, 13999, 14364, 
14364, 14364, 14364, 14729, 14729, 14729, 14729, 15094, 15094, 
15094, 15094, 15460, 15460, 15460, 15460, 15825, 15825, 15825, 
15825, 16190, 16190, 16190, 16190, 16190, 16190, 16555, 16555, 
16555, 16555, 16555, 16555, 16921, 16921, 16921, 16921, 16921, 
16921, 17286, 17286, 17286, 17286, 17651, 17651), class = "Date"), 
    end_date = structure(c(13238, 13238, 13603, 13603, 13969, 
    13969, 14334, 14334, 14334, 14334, 14699, 14699, 14699, 14699, 
    15064, 15064, 15064, 15064, 15430, 15430, 15430, 15430, 15795, 
    15795, 15795, 15795, 16160, 16160, 16160, 16160, 16525, 16525, 
    16525, 16525, 16525, 16525, 16891, 16891, 16891, 16891, 16891, 
    16891, 17256, 17256, 17256, 17256, 17256, 17256, 17621, 17621, 
    17621, 17621, 17986, 17986), class = "Date"), date = structure(c(1114819200, 
    1143763200, 1146355200, 1175299200, 1177891200, 1206921600, 
    1209513600, 1238457600, 1209513600, 1238457600, 1241049600, 
    1269993600, 1241049600, 1269993600, 1272585600, 1301529600, 
    1272585600, 1301529600, 1304121600, 1333152000, 1304121600, 
    1333152000, 1335744000, 1364688000, 1335744000, 1364688000, 
    1367280000, 1396224000, 1367280000, 1396224000, 1398816000, 
    1427760000, 1398816000, 1427760000, 1398816000, 1427760000, 
    1430352000, 1459382400, 1430352000, 1459382400, 1430352000, 
    1459382400, 1461974400, 1490918400, 1461974400, 1490918400, 
    1461974400, 1490918400, 1493510400, 1522454400, 1493510400, 
    1522454400, 1525046400, 1525046400), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), val = c(14.7299995422363, 28.4799995422363, 
    26.6800003051758, 27.5599994659424, 30.5400009155273, 27.2700004577637, 
    23.3999996185303, 12.539999961853, 23.3999996185303, 12.539999961853, 
    15.0699996948242, 24.8199996948242, 15.0699996948242, 24.8199996948242, 
    24.3799991607666, 27.7199993133545, 24.1499996185303, 32.9000015258789, 
    26.0400009155273, 18.25, 32.8499984741211, 20.1900005340576, 
    15.4499998092651, 18.3899993896484, 20.3999996185303, 27.5900001525879, 
    17.8600006103516, 25.9500007629395, 28.4099998474121, 27.8700008392334, 
    25.8999996185303, 30.7000007629395, 28.3500003814697, 24.5300006866455, 
    28.3500003814697, 24.5300006866455, 30.2399997711182, 23.2700004577637, 
    23.9699993133545, 28, 23.9699993133545, 28, 24.0400009155273, 
    33.6300010681152, 26.5200004577637, 35.3499984741211, 26.5200004577637, 
    35.3499984741211, 35.9900016784668, 44.1100006103516, 36.6500015258789, 
    39, 43.2999992370605, 34)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -54L))

Additional Calcualtions (for constructing the expected output diff column): 其他计算(用于构造预期的输出diff列):

> (27.6 - 26.7) / 26.7
[1] 0.03370787
> (27.3 - 30.5) / 30.5
[1] -0.104918
> (12.5 - 23.4) / 23.4
[1] -0.465812

Here's a not-too-pretty solution combining a dplyr call within another one. 这是一个不太漂亮的解决方案,在另一个解决方案中结合了dplyr调用。 Or, you could separate the data into the start date values and end date values, then merge them. 或者,您可以将数据分为开始日期值和结束日期值,然后合并它们。

d %>% filter(date==end_date) %>% mutate(val.end=val) %>% 
  select(-val,-date) %>% 
  left_join(d %>% filter(date==start_date) %>% mutate(val.start=val) %>% 
      select(-val,-date),.) %>% mutate(diff=(val.end-val.start)/val.start)

# A tibble: 38 x 6
   ID       start_date end_date   val.start val.end    diff
   <chr>    <date>     <date>         <dbl>   <dbl>   <dbl>
 1 00036110 2005-04-30 2006-03-31      14.7    28.5  0.933 
 2 00036110 2006-04-30 2007-03-31      26.7    27.6  0.0330
 3 00036110 2007-04-30 2008-03-31      30.5    27.3 -0.107 
 4 00036110 2008-04-30 2009-03-31      23.4    12.5 -0.464 
 5 00036110 2008-04-30 2009-03-31      23.4    12.5 -0.464 
 6 00036110 2008-04-30 2009-03-31      23.4    12.5 -0.464 
 7 00036110 2008-04-30 2009-03-31      23.4    12.5 -0.464 
 8 00036110 2009-04-30 2010-03-31      15.1    24.8  0.647 
 9 00036110 2009-04-30 2010-03-31      15.1    24.8  0.647 
10 00036110 2009-04-30 2010-03-31      15.1    24.8  0.647 
# ... with 28 more rows

You can add the start_date and end_date to the group_by : 您可以将start_dateend_date添加到group_by

  d %>%
  group_by(ID, start_date, end_date) %>%
  summarise(diff = (val[2] - val[1]) / val[1],
            val_start = val[1],
            val_end = val[2]) %>%
  arrange(start_date, end_date)

Results in: 结果是:

   ID       start_date end_date      diff val_start val_end
   <chr>    <date>     <date>       <dbl>     <dbl>   <dbl>
 1 00036110 2005-04-30 2006-03-31  0.933       14.7    28.5
 2 00036110 2006-04-30 2007-03-31  0.0330      26.7    27.6
 3 00036110 2007-04-30 2008-03-31 -0.107       30.5    27.3
 4 00036110 2008-04-30 2009-03-31 -0.464       23.4    12.5

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM