检查数据框是否从一年的第一天开始并添加缺少的日期

时间:2018-07-10 09:48:20

标签: r

我有以下数据框:

Date_from <- c("2013-01-05","2013-05-10","2013-08-13","2013-11-19")
Date_to <- c("2013-05-07","2013-08-12","2013-11-18","2013-12-25")
y <- data.frame(Date_from,Date_to)
y$concentration <- c("1.5","2.5","1.5","3.5")
y$Date_from <- as.Date(y$Date_from)
y$Date_to <- as.Date(y$Date_to)
y$concentration <- as.numeric(y$concentration)

我正在使用以下代码检查数据框是否在一年的最后一天结束:

y<-rbind(y, c(as.character(max(as.Date(y$Date_to))+1), paste0(substr(max(as.Date(y$Date_to)), 1, 4),"-12-31")  , NA))

如何在数据框的开头进行检查:检查数据框是否在一年的第一天开始,如果没有,则将此范围添加到数据框。结果应如下所示:

Date_from    Date_to concentration
2013-01-01 2013-01-04          <NA>
2013-01-05 2013-05-07           1.5
2013-05-10 2013-08-12           2.5    
2013-08-13 2013-11-18           1.5
2013-11-19 2013-12-25           3.5
2013-12-26 2013-12-31          <NA>

1 个答案:

答案 0 :(得分:1)

我又为您提供的数据添加了一年

Date_from <- c("2013-01-05","2013-05-10","2013-08-13","2013-11-19","2014-01-05","2014-05-10","2014-08-13","2014-11-19")
Date_to <- c("2013-05-07","2013-08-12","2013-11-18","2013-12-25","2014-05-07","2014-08-12","2014-11-18","2014-12-25")
y <- data.frame(Date_from,Date_to)
y$concentration <- c("1.5","2.5","1.5","3.5","2.5","1.5","1.5","3.5")
y$Date_from <- as.Date(y$Date_from)
y$Date_to <- as.Date(y$Date_to)
y$concentration <- as.numeric(y$concentration)

将以下代码段添加到您的代码中,您将获得结果

setDT(y)
y1<-y[,.(Date_from=as.Date(as.character(max(as.Date(Date_to))+1)), Date_to=as.Date(paste0(substr(max(as.Date(Date_to)), 1, 4),"-12-31"))  , concentration=NA),by=.(Year=substr(as.Date(Date_to), 1, 4))]
y2<-y[,.(Date_from=as.Date(paste0(substr(min(as.Date(Date_from)), 1, 4),"-01-01")), Date_to=as.Date(as.character(min(as.Date(Date_from))-1))  , concentration=NA),by=.(Year=substr(as.Date(Date_from), 1, 4))]

y<-rbind(y,y1[,.(Date_to,Date_from,concentration)],y2[,.(Date_to,Date_from,concentration)])[order(Date_from,Date_to),][!(Date_to==Date_from & is.na(concentration))]

有一些更巧妙的方法可以做到这一点。