R

时间:2017-10-13 07:14:52

标签: r interpolation distribution extrapolation

我有一个数据集,其中值列的天数变化。

开始结束值天数
24-03-2011 24-05-2011 398 60
25-05-2011 21-07-2011 275 56
22-07-2011 13-09-2011 389 52
14-09-2011 18-11-2011 482 64
19-11-2011 13-01-2012 514 54
14-01-2012 19-02-2012 330 35
20-02-2012 12-04-2012 519 51
13-04-2012 24-05-2012 361 40
25-05-2012 24-06-2012 202 29

我需要的是价值列的月度数据,如月度分布

开始结束值天数
01-03-2011 31-03-2011? 31个
01-04-2011 30-04-2011? 30个
01-05-2011 31-05-2011? 31个
01-06-2011 30-06-2011? 30个
01-07-2011 31-07-2011? 31个
01-08-2011 31-08-2011? 31个
01-09-2011 30-09-2011? 30个
01-10-2011 31-10-2011? 31个
01-11-2011 30-11-2011? 30个
01-12-2011 31-12-2011? 31个
01-01-2012 31-01-2012? 31个
01-02-2012 29-02-2012? 29个
01-03-2012 31-03-2012? 31个
01-04-2012 30-04-2012? 30

我不知道它的插值/外推问题但是我已经建议了这些方法。请帮忙

1 个答案:

答案 0 :(得分:2)

您可以使用已接受的解决方案here

按天扩展数据框
library(data.table)
df2 <- setDT(df)[, list(Value = Value, 
                        date = seq(from = Start, to = End, length.out = Days)), 
                 by = 1:nrow(df)]

> df2
     nrow Value       date
  1:    1   398 2011-03-24
  2:    1   398 2011-03-25
  3:    1   398 2011-03-26
  4:    1   398 2011-03-27
  5:    1   398 2011-03-28
 ---                      
750:   16   371 2013-04-11
751:   16   371 2013-04-12
752:   16   371 2013-04-13
753:   16   371 2013-04-14
754:   16   371 2013-04-16

假设您希望在开始到结束的所有日期均匀分配原始数据框中的每个值,则可以执行以下操作:

library(dplyr)
library(lubridate)

df2 %>%

  # calculate average for each day
  group_by(nrow) %>%
  mutate(Value = Value / n()) %>%
  ungroup() %>%

  # summarize by month
  mutate(Month = format(date, "%Y-%m")) %>%
  group_by(Month) %>%
  summarise(Value = sum(Value)) %>%
  ungroup() %>%

  # derive start / end dates for each month
  mutate(Start = ymd(paste0(Month, "-1"))) %>%
  mutate(End = Start %m+% months(1) - 1) %>% 
  mutate(Days = End - Start + 1) %>% 
  select(Start, End, Value, Days) 

# A tibble: 26 x 4
        Start        End     Value    Days
       <date>     <date>     <dbl>  <time>
 1 2011-03-01 2011-03-31  53.06667 31 days
 2 2011-04-01 2011-04-30 192.36667 30 days
 3 2011-05-01 2011-05-31 186.94167 31 days
 4 2011-06-01 2011-06-30 142.41071 30 days
 5 2011-07-01 2011-07-31 173.02198 31 days
 6 2011-08-01 2011-08-31 224.42308 31 days
 7 2011-09-01 2011-09-30 217.80048 30 days
 8 2011-10-01 2011-10-31 225.93750 31 days
 9 2011-11-01 2011-11-30 242.25347 30 days
10 2011-12-01 2011-12-31 285.55556 31 days
# ... with 16 more rows

数据:

df <- read.table(
  header = T,
  stringsAsFactors = F, 
  text = "Start       End        Value  Days
24-03-2011  24-05-2011  398   60
25-05-2011  21-07-2011  275   56
22-07-2011  13-09-2011  389   52
14-09-2011  18-11-2011  482   64
19-11-2011  13-01-2012  514   54
14-01-2012  19-02-2012  330   35
20-02-2012  12-04-2012  519   51
13-04-2012  24-05-2012  361   40
25-05-2012  24-06-2012  202   29
25-05-2012  06-08-2012  691   72
07-08-2012  23-09-2012  376   46
24-09-2012  06-11-2012  300   42
07-11-2012  21-12-2012  272   43
22-12-2012  31-01-2013  276   39
01-02-2013  02-03-2013  188   28
03-03-2013  16-04-2013  371   43"
)

df$Start = as.Date(df$Start, "%d-%m-%Y")
df$End = as.Date(df$End, "%d-%m-%Y")