如何通过将值分配给不同的时间步来重复值

How to repeat values by assigning them to a different time step

我有一个具有不同时间步长的数据框:

Dataframe 1 :
02/06/2021 17:57   /    16.38 
02/06/2021 18:05   /    25.89
02/06/2021 18:11   /    21.32
02/06/2021 18:32   /    12.65
02/06/2021 19:00   /    14.78
...                    ....

我想为步骤之间的每一分钟设置相同的值,如下所示:

02/06/2021 17:57    /   16.38
02/06/2021 17:58    /   16.38
02/06/2021 17:59    /   16.38
02/06/2021 18:00    /   16.38
02/06/2021 18:01    /   16.38
02/06/2021 18:02    /   16.38
02/06/2021 18:03    /   16.38
02/06/2021 18:04    /   16.38 
02/06/2021 18:05    /   25.89 
02/06/2021 18:06    /   25.89
02/06/2021 18:07    /   25.89
02/06/2021 18:08    /   25.89
02/06/2021 18:09    /   25.89
02/06/2021 18:10    /   25.89
02/06/2021 18:11    /   21.32
02/06/2021 18:12    /   21.32
...etc

我猜这是一个“while”循环,但我是 info 的新手,无法理解它。

感谢您能给我的任何帮助!

再见

使用 completefill 在这里会有帮助 -

library(dplyr)
library(lubridate)
library(tidyr)

df %>%
  mutate(datetime = dmy_hm(datetime)) %>%
  complete(datetime = seq(min(datetime), max(datetime), by = '1 min')) %>%
  fill(value)

#   datetime            value
#   <dttm>              <dbl>
# 1 2021-06-02 17:57:00  16.4
# 2 2021-06-02 17:58:00  16.4
# 3 2021-06-02 17:59:00  16.4
# 4 2021-06-02 18:00:00  16.4
# 5 2021-06-02 18:01:00  16.4
# 6 2021-06-02 18:02:00  16.4
# 7 2021-06-02 18:03:00  16.4
# 8 2021-06-02 18:04:00  16.4
# 9 2021-06-02 18:05:00  25.9
#10 2021-06-02 18:06:00  25.9
# … with 54 more rows

不清楚你的日期格式是什么。我假设你的数据是 dmy 格式,如果你有 mdy 格式的日期,那么使用 mdy_hm 函数。

数据

如果您在 reproducible format

中提供数据,会更容易提供帮助
df <- structure(list(datetime = c("02/06/2021 17:57   ", "02/06/2021 18:05   ", 
"02/06/2021 18:11   ", "02/06/2021 18:32   ", "02/06/2021 19:00   "
), value = c(16.38, 25.89, 21.32, 12.65, 14.78)), 
class = "data.frame", row.names = c(NA, -5L))