I am dealing with time series data where I need to have continuous time stamps but few of the data timestamp points has been missed while capturing like as below,
DF
ID Time_Stamp A B C
1 02/02/2018 07:45:00 123 567 434
2 02/02/2018 07:45:01
..... ...
5 02/02/2018 07:46:00
6 02/02/2018 07:46:10 112 2323 2323
As shown in the sample df above, time stamps is continuous till row 5 but missed capturing data of 10 seconds between 5th and 6th row. My data frame is about 60000 rows and identifying missing values manually is tedious. Hence I was looking for automating the procedure of handling missing values using R
My result data frame should be as shown below,
ID Time_Stamp A B C
1 02/02/2018 07:45:00 123 567 434
2 02/02/2018 07:45:01
..... ...
5 02/02/2018 07:46:00 123 567 434
5.1 02/02/2018 07:46:01 123 567 434
5.2 02/02/2018 07:46:02 123 567 434
5.3 02/02/2018 07:46:03 123 567 434
5.4 02/02/2018 07:46:04 123 567 434
5.5 02/02/2018 07:46:05 123 567 434
5.6 02/02/2018 07:46:06 123 567 434
5.7 02/02/2018 07:46:07 123 567 434
5.8 02/02/2018 07:46:08 123 567 434
5.9 02/02/2018 07:46:09 123 567 434
6 02/02/2018 07:46:10 112 2323 2323
6.1 02/02/2018 07:46:11 112 2323 2323
i.e my last row before missing value . It is came values that for row number 5 and should fill same till next value is available.
I tried following code to add 0
as values to it. But could not do for previous row values of missing value
df1<- df %>%
complete(Timestamp = seq(min(Timestamp), max(Timestamp), by = "sec")) %>%
mutate_at(vars(A:C), ~replace(., is.na(.), 0 )) %>%
mutate(ID = row_number())
I got output as follows:
ID Time_Stamp A B C
1 02/02/2018 07:45:00 123 567 434
2 02/02/2018 07:45:01
..... ...
5 02/02/2018 07:46:00 123 567 434
5.1 02/02/2018 07:46:01 0 0 0
5.2 02/02/2018 07:46:02 0 0 0
5.3 02/02/2018 07:46:03 0 0 0
5.4 02/02/2018 07:46:04 0 0 0
5.5 02/02/2018 07:46:05 0 0 0
5.6 02/02/2018 07:46:06 0 0 0
5.7 02/02/2018 07:46:07 0 0 0
5.8 02/02/2018 07:46:08 0 0 0
5.9 02/02/2018 07:46:09 0 0 0
6 02/02/2018 07:46:10 112 2323 2323
6.1 02/02/2018 07:46:11 0 0 0
I want previous row to be replaced with 0 value.
Thanks in advance