With data.table, I have a column with many NA, I would like to replace the NA's with prior non-NA value. I can shift to prior one at certain place, but can't really locate on the nearest prior one. Please advice, and here is the toy example.
x <- data.table(A = 1:5, B.1 = c(1,NA,NA,2,NA), B.2 = c(1,1,1,2,2))
x[,B.3 := ifelse(is.na(B.1), shift(B.1, 1), B.1)]
#   A B.1 B.2  B.3
#1: 1   1   1  1
#2: 2  NA   1  1
#3: 3  NA   1 NA
#4: 4   2   2  2
#5: 5  NA   2  2
B.1 is my target column, and B.2 is what I wanna get. As you can see, x$B.1[2] is following x$B.1[1], x$B.1[3] also follows x$B.1[1], since x$B.1[1] is the nearest prior non-NA value. So, my attention is to make B.2, but I only can do B.3.
 
    