概述
seq.Date()
的错误消息试图告诉您Moves$End
(即 2017 年 6 月 23 日)的日期发生在 Moves$Start
(即 2017 年 4 月 17 日)之前。因为seq.Date()
假定 in 中的所有日期都from
发生在 中的日期之前to
,所以该错误会阻止函数继续执行。
要确定发生这种情况的位置,请使用which()
来确定哪些日期Moves$End
小于Moves$Start
。从那里,更新这些日期,使它们发生在 Moves$Start
.
# load necessary data
Moves <- data.frame( Start = as.Date( x = c("2017-04-17", "2018-03-01", "2019-04-01") )
, End = as.Date( x = c("2017-06-23", "2018-02-14", "2018-04-24") )
, stringsAsFactors = FALSE )
# try to create a sequence of dates
date.ranges <-
mapply( FUN = function( mi, ma )
seq.Date( from = mi
, to = ma
, by = "day" )
, Moves$Start
, Moves$End
, SIMPLIFY = FALSE )
# identify the instance
# where the End date occurs
# before the Start date
wrong.end.date <-
which( Moves$End < Moves$Start )
# view results
wrong.end.date
# [1] 2 3
# correct those End Dates
# so that they occur
# after the Start date
Moves$End[ wrong.end.date ] <-
as.Date( x = c("2019-02-14", "2019-04-24") )
# rerun the mapply() function
date.ranges <-
mapply( FUN = function( mi, ma )
seq.Date( from = mi
, to = ma
, by = "day" )
, Moves$Start
, Moves$End
, SIMPLIFY = FALSE )
# end of script #