r - 填写缺失的日期范围
问题描述
我有以下示例数据框:
Date_from <- c("2013-01-01","2013-01-10","2013-01-16","2013-01-19")
Date_to <- c("2013-01-07","2013-01-12","2013-01-18","2013-01-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)
这些是特定日期范围内重金属的测量浓度。但是,日期范围不连续,因为 2013-01-07 到 2013-01-10 和 2013-01-12 到 2013-01-16 之间存在间隔。我需要检测这些间隙,在每个间隙后插入一行并用缺失的范围填充它。结果应如下所示:
Date_from Date_to concentration
2013-01-01 2013-01-07 1.5
2013-01-08 2013-01-09 NA
2013-01-10 2013-01-12 2.5
2013-01-13 2013-01-15 NA
2013-01-16 2013-01-18 1.5
2013-01-19 2013-01-25 3.5
解决方案
尝试这个:
adding <- data.frame(Date_from = y$Date_to[-nrow(y)]+1,
Date_to = y$Date_from[-1]-1, concentration = NA)
adding <- adding[adding$Date_from <= adding$Date_to,]
res <- rbind(y,adding)
res[order(res$Date_from),]
# Date_from Date_to concentration
#1 2013-01-01 2013-01-07 1.5
#5 2013-01-08 2013-01-09 NA
#2 2013-01-10 2013-01-12 2.5
#6 2013-01-13 2013-01-15 NA
#3 2013-01-16 2013-01-18 1.5
#4 2013-01-19 2013-01-25 3.5
推荐阅读
- scala - 如何在异常时停止 Actor 重新加载
- c# - 在c#中运行更新查询后数据库没有变化
- python - 检查某个数字序列是否出现在数组中。我的想法有什么问题?
- c# - 在函数调用 Visual Studio 键盘快捷键中包装变量
- javascript - 通过 WKWebView 使用 Javascript、AngularJS 在设备上保存数据以自动填充表单
- wordpress - 在 Wordpress 的一般部分中添加字段
- sql-server - TFS 2015 - 在 SQL Server 中跟踪“正在做和完成”子列
- excel - 通过 udf 应用条件格式时遇到问题
- node.js - 如何让所有快递路由器都使用验证码
- c - 我的系统的目标三元组是什么?