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 is as 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 mean(A1:A5)
5.1 02/02/2018 07:46:01 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.2 02/02/2018 07:46:02 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.3 02/02/2018 07:46:03 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.4 02/02/2018 07:46:04 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.5 02/02/2018 07:46:05 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.6 02/02/2018 07:46:06 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.7 02/02/2018 07:46:07 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.8 02/02/2018 07:46:08 mean(A1:A5) mean(B1:B5) mean(C1:C5)
5.9 02/02/2018 07:46:09 mean(A1:A5) mean(B1:B5) mean(C1:C5)
6 02/02/2018 07:46:10 112 2323 2323
6.1 02/02/2018 07:46:11 mean(A1:A15) mean(B1:B15) mean(C1:C15)
Or even it can be the mean of previous rows in that time interval.
6.1 02/02/2018 07:46:11 mean(A14:A17) mean(B14:B17) mean(C14:C17)
I.e missing except missing time values .
I have done following code to get mean of whole column.
library(dplyr)
library(tidyr)
df %>%
complete(Time_Stamp = seq(min(Time_Stamp), max(Time_Stamp), by = "sec")) %>%
mutate_at(vars(A:C), ~replace(., is.na(.), mean(., na.rm = TRUE))) %>%
mutate(ID = row_number())
It gives output for all mean of all the row in the column .
Like following this code It worked perfect but i need this modification . How can do it. kindly Help