Bolle
Bolle

Reputation: 322

compare rows and fill gaps with previous values in a data table

I have data table that looks like this:

library(data.table)
data <- data.table(time = c(seq(0, 14)),
                   anom = c(0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0),
                   gier = c(0, 0, 0, 4, 9, 7, 2, 0, 3, 1, 4, 2, 0, 0, 0))

Now I want to fill the gaps (zeros) with ones in column anom so that the result looks like this:

res <- data.table(time = c(seq(0, 14)),
                  anom = c(0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0),
                  gier = c(0, 0, 0, 4, 9, 7, 2, 0, 3, 1, 4, 2, 0, 0, 0))

In addition there is the problem that I want to be flexible with the gap size so I can choose how big the gap can be. There must be an easy way to do something similar to LOCF only for real values (maybe filling it with the previous value of the row and not only ones or zeros) and not only for NA's like the functions fill or na.locf.

Upvotes: 1

Views: 325

Answers (2)

chinsoon12
chinsoon12

Reputation: 25225

Here is another option using rolling join:

maxgap <- 1L
data[, c("rn", "lu") := .(.I, anom)]
data[anom==0L, lu := fcoalesce(
    data[anom!=0L][.SD, on=.(rn=rn), roll=maxgap, rollends=c(FALSE, FALSE), x.anom],
    anom)
]

output:

    time anom gier rn lu
 1:    0    0    0  1  0
 2:    1    0    0  2  0
 3:    2    0    0  3  0
 4:    3    1    4  4  1
 5:    4    1    9  5  1
 6:    5    1    7  6  1
 7:    6    0    2  7  1
 8:    7    0    0  8  0
 9:    8    1    3  9  1
10:    9    0    1 10  1
11:   10    1    4 11  1
12:   11    1    2 12  1
13:   12    0    0 13  0
14:   13    0    0 14  0
15:   14    0    0 15  0

Upvotes: 0

user2974951
user2974951

Reputation: 10375

An example using the maxgap argument to select the maximum gap size

library(zoo)
na.fill(
  na.locf(
    replace(data$anom,data$anom==0,NA),
    na.rm=F,
    maxgap=2
  ),0
)

 [1] 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0

Upvotes: 1

Related Questions