R语言 使用which()查找特定posix时间之间的值,并在没有数据时填充NA

0wi1tuuw  于 2023-02-27  发布在  其他
关注(0)|答案(3)|浏览(112)

我有一个 Dataframe ,看起来像这样:

dat <- data.frame("posix_time" = as.POSIXct(c("2021-07-01 01:00:00 CEST", "2021-07-01 01:10:00 CEST", "2021-07-01 01:20:00 CEST", 
                                   "2021-07-01 01:30:00 CEST", "2021-07-01 01:40:00 CEST", "2021-07-01 01:50:00 CEST",
                                   "2021-07-01 02:00:00 CEST", "2021-07-01 02:10:00 CEST", "2021-07-01 02:20:00 CEST")),
                  "value" = c(5, 8, 15, 7, 12, 5, 89, 1, 17))

现在我想直接用which函数得到索引,或者这里是“value”的数据,这样做很好:

temp <- dat$value[which(dat$posix_time >= "2021-07-01 01:00:00" & dat$posix_time <= "2021-07-01 02:00:00")]

然后应将其写入特定大小的预定义矩阵中:

mat <- matrix(NA, ncol = 15, nrow = 1)

其中,ncol是时间步长的数量,如下所示:

colnames(mat) <- c("00:00", "00:10", "00:20", "00:30", "00:40", "00:50", "01:00", "01:10", "01:20", "01:30", "01:40", "01:50", "02:00", "02:10", "02:20")

现在我想把dat的正确时间对应的值写入矩阵,但我不知道该怎么做。
它应该如下所示:

00:00 00:10 00:20 00:30 00:40 00:50 01:00 01:10 01:20 01:30 01:40 01:50 02:00 02:10 02:20
NA    NA    NA    NA    NA    NA     5     8    15     7    12     5    89    NA    NA
uinbv5nw

uinbv5nw1#

dat <- dat[which(dat$posix_time >= "2021-07-01 01:00:00" & dat$posix_time <= "2021-07-01 02:00:00"), ]
mat[match(gsub("^.* (\\d{2}:\\d{2}).*$", "\\1", dat$posix_time), colnames(mat))] <- dat$value

#     00:00 00:10 00:20 00:30 00:40 00:50 01:00 01:10 01:20 01:30 01:40 01:50 02:00 02:10 02:20
#[1,]    NA    NA    NA    NA    NA    NA     5     8    15     7    12     5    89    NA    NA
tjvv9vkg

tjvv9vkg2#

首先,在一定时间范围内的子集dat

dat2 <- dat[dat$posix_time >= "2021-07-01 01:00:00" & dat$posix_time <= "2021-07-01 02:00:00", ]

然后可以使用列名mat作为索引。

mat[, strftime(dat2$posix_time, "%H:%M")] <- dat2$value
mat

#      00:00 00:10 00:20 00:30 00:40 00:50 01:00 01:10 01:20 01:30 01:40 01:50 02:00 02:10 02:20
# [1,]    NA    NA    NA    NA    NA    NA     5     8    15     7    12     5    89    NA    NA
qmelpv7a

qmelpv7a3#

我们可以使用format来获得我们想要的格式的日期,然后进行匹配,即

dat$value[match(colnames(mat), format(dat$posix_time, '%H:%M'))]
# [1] NA NA NA NA NA NA  5  8 15  7 12  5 89  1 17

相关问题