simple dplyr mutate question

I have a data frame containing a variable with interspersed NAs.

I want to replace/ create a new variable replacing the NAs with the last non-NA value...I've been trying to come up with a dplyr::mutate consturction, but maybe a base R soln is better.

in any case I'm blocked up on this and would appreciate any help. see examples below:

df_i_have <- data.frame(obs = LETTERS[1:10], value = c(1:3, NA, NA, 4:5, NA, 6:7)) 

df_i_want <- data.frame(obs = LETTERS[1:10], value = c(1:3, 3, 3, 4:5, 5, 6:7))

Created on 2018-11-12 by the reprex package (v0.2.1)

I'm sure one could come up with a dplyr solution, but there is a simple function in the zoo package that will do this. Might be your easiest route.

zoo::na.locf(df_i_have)
5 Likes

There's also fill() from tidyr (which is included with dplyr in the tidyverse)

df_i_have <- data.frame(obs = LETTERS[1:10], value = c(1:3, NA, NA, 4:5, NA, 6:7)) 
tidyr::fill(df_i_have, value)
#>    obs value
#> 1    A     1
#> 2    B     2
#> 3    C     3
#> 4    D     3
#> 5    E     3
#> 6    F     4
#> 7    G     5
#> 8    H     5
#> 9    I     6
#> 10   J     7

Created on 2018-11-13 by the reprex package (v0.2.0).

8 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.