Home > Software engineering >  Fill Timeseries with founded values or keep NA if no value was found yet
Fill Timeseries with founded values or keep NA if no value was found yet

Time:12-18

I had a longer TimeSeries and turned it into wider for forecasting purposes, currently timeseries has the following structure :

Day Value Strength1 Strength2 Strength3
1/2 1.356 3 NA NA
2/2 1.385 NA NA NA
3/2 1.385 NA 1.01 NA
4/2 1.4 NA NA 10
5/2 1.6 NA NA NA
6/2 1.7 4 NA NA
7/2 1.8 NA 1.05 NA
8/2 1.88 NA NA NA
9/2 1.98 NA NA 11
10/2 1.8 NA NA NA

I want a function that :

  • given a TimeSeries
  • loops through columns if cell == NA and previously only NAs were found in the column , keep NA
  • if cell != NA good
  • if cell == NA But previously we found not NA values, change to previously found value

This would be result :

Day Value Strength1 Strength2 Strength3
1/2 1.356 3 NA NA
2/2 1.385 3 NA NA
3/2 1.385 3 1.01 NA
4/2 1.4 3 1.01 10
5/2 1.6 3 1.01 10
6/2 1.7 4 1.01 10
7/2 1.8 4 1.05 10
8/2 1.88 4 1.05 10
9/2 1.98 4 1.05 11
10/2 1.8 4 1.05 11

I tried this function but it isn't right :

filler <- function(df) {
  col <- colnames(df)
  one <- NA
  for (i in col) {
    for (a in i) {
      if(!is.na(a)) {
        one = a
      }
      if(!is.na(one) & is.na(a)) {
        a = one
      }
    }
  }
} 

CodePudding user response:

You may use tidyr::fill -

filler <- function(data) tidyr::fill(data, dplyr::everything())
filler(df)

#    Day Value Strength1 Strength2 Strength3
#1   1/2 1.356         3        NA        NA
#2   2/2 1.385         3        NA        NA
#3   3/2 1.385         3      1.01        NA
#4   4/2 1.400         3      1.01        10
#5   5/2 1.600         3      1.01        10
#6   6/2 1.700         4      1.01        10
#7   7/2 1.800         4      1.05        10
#8   8/2 1.880         4      1.05        10
#9   9/2 1.980         4      1.05        11
#10 10/2 1.800         4      1.05        11
  • Related