【发布时间】:2017-01-26 01:32:57
【问题描述】:
我的数据包含一列订单日期。它还有一列交货日期。一些交货日期是在订单日期之前发生的日期(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
【问题讨论】: