0

我有一组与ID我们的主要结果测量 ( Y) 和协变量 ( X1) 相对应的个人 ( ) 日期和时间。

我的目标是如果测量是在变量测量日期/时间的 +/- 24 小时内记录的,则替换每一行的缺失X1值。为了使这更容易可视化(并加载到 R 中),以下是数据当前的排列方式:YX1Y

structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), TIME = structure(1:15, .Label = c("01/01/2013 12:01", 
"01/03/2013 08:49", "01/03/2013 20:52", "02/01/2013 05:00", "02/03/2013 05:30", 
"02/03/2013 21:14", "02/05/2013 05:15", "02/12/2013 05:03", "02/15/2013 04:16", 
"02/16/2013 04:12", "02/16/2013 21:02", "03/01/2010 17:58", "03/02/2010 00:10", 
"03/03/2010 10:45", "03/04/2010 09:00"), class = "factor"), Y = structure(c(1L, 
5L, 7L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 3L, 1L, 8L, 1L, 6L), .Label = c(".", 
"22", "35", "4", "5", "6", "8", "9"), class = "factor"), X1 = structure(c(2L, 
1L, 1L, 7L, 7L, 1L, 4L, 4L, 3L, 1L, 1L, 6L, 1L, 5L, 1L), .Label = c(".", 
"0.1", "0.2", "0.4", "0.6", "0.9", "1.0"), class = "factor")), .Names = c("ID", 
"TIME", "Y", "X1"), class = "data.frame", row.names = c(NA, -15L))

为了简化所需的输出,我只想显示具有非缺失Y值的行,以便最终产品如下所示:

  ID             TIME  Y  X1
1  1 01/03/2013 08:49  5   .
2  1 01/03/2013 20:52  8   .
3  2 02/03/2013 21:14 22   .
4  2 02/16/2013 04:12  4 0.2
5  2 02/16/2013 21:02 35   .
6  3 03/02/2010 00:10  9 0.9
7  3 03/04/2010 09:00  6 0.6

是否可以 (1) 遍历多行并评估 24 小时的绝对值以获得X1Y测量值之间的差异,以及 (2) 用X1+/- 24 小时窗口内的缺失值替换缺失值?

任何有关如何解决此问题的想法将不胜感激!

4

1 回答 1

0

如果你将你的数据转换成xts,那么你可以使用 xts 的简单子集功能来获得你想要的。

PS:如果您在 Y 测量的 24 小时内恰好有 1 个 X1 值,则以下代码将起作用。

require(xts)
xx <- xts(DF[, c(1, 4, 5)], as.POSIXct(paste0(DF$Date, " ", DF$TIME), format = "%m/%d/%Y %H:%M"))


sapply(index(xx[!is.na(xx$Y)]), FUN = function(tt) {
    startTime <- tt - 24 * 60 * 60
    endTime <- tt + 24 * 60 * 60
    y <- xx[paste(startTime, endTime, sep = "/")]
    if (nrow(y[!is.na(y$X1), "X1"]) != 0) {
        return(as.vector(y[!is.na(y$X1), "X1"]))
    } else {
        return(NA)
    }

})
## [1] 0.9 0.6  NA  NA 1.0 0.2  NA


xx[!is.na(xx$Y), "X1"] <- sapply(index(xx[!is.na(xx$Y)]), FUN = function(tt) {
    startTime <- tt - 24 * 60 * 60
    endTime <- tt + 24 * 60 * 60
    y <- xx[paste(startTime, endTime, sep = "/")]
    if (nrow(y[!is.na(y$X1), "X1"]) != 0) {
        return(as.vector(y[!is.na(y$X1), "X1"]))
    } else {
        return(NA)
    }

})

xx[!is.na(xx$Y), "X1"]
##                      X1
## 2010-03-02 00:10:00 0.9
## 2010-03-04 09:00:00 0.6
## 2013-01-03 08:49:00  NA
## 2013-01-03 20:52:00  NA
## 2013-02-03 21:14:00 1.0
## 2013-02-16 04:12:00 0.2
## 2013-02-16 21:02:00  NA
于 2013-10-25T02:27:05.653 回答