2

I have this vector

set.seed(234)
x <- sample(c(rep(NA,3),1:5))
x
[1]  3  5 NA  1  4 NA NA  2

For each NA, I want the index (or value) of the last preceeding non-NA value. That is, for the first NA, the last previous non-NA has the index 2. For the next two NA, their last previous non-NA has index 5:

[1]  NA  NA 2  NA  NA 5 5  NA

Base R or tidyverse would be ok. I tried combinations of lag, lead, rle, gl & coalesce, but all without success. For instance this which is pretty near, but still wrong.

a <- rle(is.na(x))
rep(1:length(a$lengths), a$lengths)
[1] 1 1 2 3 3 4 4 5
Henrik
  • 65,555
  • 14
  • 143
  • 159
Roman
  • 17,008
  • 3
  • 36
  • 49
  • Can you confirm that the values you want are correct? The way I'm reading "last non-NA" value would imply that the string you want is -- NA NA 5 NA NA 4 4 NA. In the third position, wouldn't 5 be the last non-na value? – Pawel Nov 16 '18 at 15:00
  • @Pawel yes, indeces or values are both ok. – Roman Nov 16 '18 at 15:01
  • @Pawel, I think he means he wants to make for each NA the index for the last non-NA value that preceded it. – iod Nov 16 '18 at 15:02
  • Yep. Sorry. Re-read it and it make sense. – Pawel Nov 16 '18 at 15:06

2 Answers2

7

Using base R

y=1:length(x)
y[is.na(x)]=0
y=cummax(y)
y[!is.na(x)]=NA

y
[1] NA NA  2 NA NA  5  5 NA

squeeze to one_line from Henrik

replace(cummax(seq_along(x) * !is.na(x)), !is.na(x), NA)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Here's a way to get the values with the tidyr function fill

library(tidyr)
fill(as.data.frame(x),x)[is.na(x),]
#[1] 5 4 4
jasbner
  • 2,253
  • 12
  • 24
  • this! `tibble(x) %>% fill(x)` . Thanks for the function. – Roman Nov 16 '18 at 15:13
  • @Jimbou If you 'only' want to replace the `NA` with previous non-`NA` (as e.g. `fill` does), see [Replacing NAs with latest non-NA value](https://stackoverflow.com/questions/7735647/replacing-nas-with-latest-non-na-value) – Henrik Nov 16 '18 at 15:49