2017-03-08 54 views
1

我有日常銷售數據。根據兩套日期不同的日期變換值

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年的價值的10% 01-11,然後每個'價值'+ 2014-01-12到2014-01-20(等等)的價值的15%。

回答

2

你可以試試:

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

完美!非常感謝你 –