检测范围内的双日期并调整日期

时间:2019-01-24 10:04:49

标签: r date dplyr

我有以下数据框:

library(hydroGOF)
library(ModelMetrics)
library(dplyr)
Date_from <- c("2013-01-01","2013-01-04","2013-01-06","2013-01-11")
Date_to <- c("2013-01-03","2013-01-06","2013-01-10","2013-01-14")
Parameter <- c("Par1","Par1","Par1","Par1")
conc<-c("1.5","2.5","1.5","1.8")
metals<-data.frame(Date_from,Date_to,Parameter,conc)
metals$Date_from<-as.Date(metals$Date_from)
metals$Date_to<-as.Date(metals$Date_to)
metals$conc<-as.numeric(as.character(metals$conc))

我需要在日期范围内检测到错误的开始日期。在此示例中,这是从2013-01-06开始的第三个范围,它是第二个日期范围的结束日期。我需要代码来检测到这一点并调整上一个日期范围结束后一天的开始日期。结果应如下所示:

Date_from    Date_to Parameter conc
2013-01-01 2013-01-03      Par1  1.5
2013-01-04 2013-01-06      Par1  2.5
2013-01-07 2013-01-10      Par1  1.5
2013-01-11 2013-01-14      Par1  1.8

3 个答案:

答案 0 :(得分:1)

您可以尝试:

ind = which(difftime(metals$Date_to,lead(metals$Date_from))==0)+1 #search for time difference == 0, save the index

metals[ind,"Date_from"] = metals[ind,"Date_from"] + 1 # add a day to the start date

答案 1 :(得分:1)

我们可以使用dplyr::lag

 library(dplyr)
 metals %>% mutate(Date_from_u=lag(Date_to,default=0), 
                   Date_from_corr=if_else(Date_from==Date_from_u,Date_from+1,Date_from))

答案 2 :(得分:1)

lubridate在这里。关键是使用if_else而不是ifelse

library(dplyr)
library(lubridate)
metals %>% 
  mutate_if(is.Date,as.Date.character) %>% 
  mutate(Date_from=ymd(Date_from),Date_to=ymd(Date_to),
         Date_from=if_else(Date_from%in%Date_to,Date_from+1,Date_from))


# Date_from    Date_to    Parameter conc
#1 2013-01-01 2013-01-03      Par1  1.5
#2 2013-01-04 2013-01-06      Par1  2.5
#3 2013-01-07 2013-01-10      Par1  1.5
#4 2013-01-11 2013-01-14      Par1  1.8