Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 查找日期间隔包含准时日期的值_R_Data.table - Fatal编程技术网

R 查找日期间隔包含准时日期的值

R 查找日期间隔包含准时日期的值,r,data.table,R,Data.table,我有一个理论值的数据表,给出了一个区间: firstDate lastDate theoric 2017-01-01 2017-01-03 10 2017-01-05 2017-01-25 20 2017-02-01 2017-08-31 30 另一方面,我有精确的测量值: datetime measured 2017-01-02 11 2017-01-08 22 2017-01-09 19 2017-01-26

我有一个理论值的数据表,给出了一个区间:

firstDate   lastDate    theoric
2017-01-01  2017-01-03  10
2017-01-05  2017-01-25  20
2017-02-01  2017-08-31  30
另一方面,我有精确的测量值:

datetime      measured
2017-01-02       11
2017-01-08       22
2017-01-09       19
2017-01-26       25
2017-03-02       32
对于每个测量值,我想得到相应的理论值(其间隔包括测量日期)

注:1。理论区间不能重叠。2.如果测量值不在任何理论间隔内,则返回NA

预期产出:

datetime    measured  theoric
2017-01-02  11        10
2017-01-08  22        20
2017-01-09  19        20
2017-01-26  25        NA
2017-03-02  32        30
可复制数据集:

theoricDt <- structure(list(firstDate = structure(c(1483228800, 1483574400, 1485907200), class = c("POSIXct", "POSIXt"), tzone = "GMT"),     lastDate = structure(c(1483401600, 1485302400, 1504137600 ), class = c("POSIXct", "POSIXt"), tzone = "GMT"), theoric = c(10, 20, 30)), .Names = c("firstDate", "lastDate", "theoric"), row.names = c(NA, -3L), class = c("data.table", "data.frame"))
measureDt <- structure(list(datetime = structure(c(1483315200, 1483833600, 1483920000, 1485388800, 1488412800), class = c("POSIXct", "POSIXt"), tzone = "GMT"), measured = c(11, 22, 19, 25, 32)), .Names = c("datetime", "measured"), row.names = c(NA, -5L), class = c("data.table","data.frame"))

theoricDt您可以使用非等联接:

measureDt[theoricDt, on = .(datetime >= firstDate, datetime <= lastDate),
          theoric := i.theoric]

measureDt
#     datetime measured theoric
#1: 2017-01-02       11      10
#2: 2017-01-08       22      20
#3: 2017-01-09       19      20
#4: 2017-01-26       25      NA
#5: 2017-03-02       32      30
measureDt[Theoriicdt,on=(datetime>=firstDate,datetime