I have a dataframe with different time steps:
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
... ....
I would like to put the same value for every minute between steps, like this:
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
I guess it's a "while" loop, but I'm new to info and I'm having trouble getting through it.
Thanks for any help you can give me!
Bye
CodePudding user response:
Using complete
and fill
will be helpful here -
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
It is not clear what is the format of your date. I have assumed your data is in dmy
format, if you have dates in mdy
format then use mdy_hm
function.
data
It is easier to help if you provide data in a 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))