基于不按顺序的两组日期转换值

时间:2017-03-08 09:32:50

标签: r date dataframe

我有每日销售数据。

set.seed(24)
Values <- as.data.frame(matrix(sample(0:200,70*10, replace=TRUE), ncol=1))
Daily <- as.data.frame(seq(as.Date("2014/1/1"), by = "day", length.out = 700))
Sales <- cbind(Values,Daily)
colnames(Sales) <- c("values","daily")

我还有多少即将到来的销售数据(从工资中)丢失,并且预计每月三次(不是定期)。这表示为每日销售额的百分比。

Three_month <- c('2014-01-01','2014-01-12','2014-01-21','2014-02-03','2014-02-11','2014-02-20', 
      '2014-03-04','2014-03-13','2014-03-20','2014-04-01','2014-04-11','2014-04-23',
      '2014-05-05','2014-05-12','2014-05-21','2014-06-03','2014-06-11','2014-06-20', 
      '2014-07-04','2014-07-13','2014-07-20','2014-08-01','2014-08-11','2014-08-23',
      '2014-09-05','2014-09-12','2014-09-21','2014-10-03','2014-10-11','2014-10-20', 
      '2014-11-04','2014-11-13','2014-11-20','2014-12-01','2014-12-11','2014-12-23', 
      '2015-01-05','2015-01-12','2015-01-21','2015-02-03','2015-02-11','2015-02-20', 
      '2015-03-04','2015-03-13','2015-03-20','2015-04-01','2015-04-11','2015-04-23',
      '2015-05-05','2015-05-12','2015-05-21','2015-06-03','2015-06-11','2015-06-20', 
      '2015-07-04','2015-07-13','2015-07-20','2015-08-01','2015-08-11','2015-08-23',
      '2015-09-05','2015-09-12','2015-09-21','2015-10-03','2015-10-11','2015-10-20', 
      '2015-11-04','2015-11-13','2015-11-20','2015-12-01','2015-12-11','2015-12-23')
Three_month <- as.data.frame(Three_month)
Three_month$Three_month <- as.Date(Three_month$Three_month)
Percentage <- as.data.frame(matrix(sample(0:15,7.2*10, replace=TRUE), ncol=1))
Losses <- cbind(Three_month,Percentage)
colnames(Losses) <- c("three_month","percentage")

我的问题是如何才能获得预计的每日销售额,其中百分比损失会重新添加到原始值?因此,例如,如果在“损失”数据框中,前两个值是10%和15%,我如何创建一个变量来计算2014-01-01到2014-01-11的每个'值'+ 10%的值,然后每个'值'+ 2014-01-01的值的15% -12至2014-01-20(依此类推)。

1 个答案:

答案 0 :(得分:2)

您可以尝试:

Sales$newcol<-Sales$values*
        (Losses$percentage[findInterval(Sales$daily,Losses$three_month)]/100+1)