5
我的数据包含一列订单日期。它也有一列交货日期。一些交货日期是在订单日期之前发生的日期(12/31/1990),这在计算平均运输时间方面造成问题。我想对这些行采取订单日期,并从统一分配中添加随机数。通过某些功能给日期添加随机天数
首先,我试图编写一个函数,可以将其应用于数据,但结果不是我想要的。我想要的是模拟交货日期在交货日期栏中结束。
func1 = function(x){
if(x[2]=="1990-12-31" && !is.na(x[2]))
x[2] = as.Date(x[1]) + floor(runif(1,min=0,max=30))
return (x)
}
示例数据:
x <- structure(list(orderDate = structure(c(15706, 15706, 15706, 15706,
15706), class = "Date"), deliveryDate = structure(c(15707, 15707,
7669, 15707, 7669), class = "Date")), .Names = c("orderDate",
"deliveryDate"), row.names = c(NA, 5L), class = "data.frame")
# orderDate deliveryDate
#1 2013-01-01 2013-01-02
#2 2013-01-01 2013-01-02
#3 2013-01-01 1990-12-31
#4 2013-01-01 2013-01-02
#5 2013-01-01 1990-12-31