Completing the Nas of a Tibble

Asked

Viewed 13 times

-2

Hello, I have a database on R where there are some Nas in the variables. I would like to apply a logical function where Nas would be filled with the immediately preceding value. Below is an example:

dados <- tibble::tibble(x = c(2, 3, 5, NA, 2, 1, NA, NA, 9, 3), 
                        y = c(4, 1, 9, NA, 8, 5, NA, NA, 1, 2)
)

print(dados)

# A tibble: 10 x 2
       x     y
   <dbl> <dbl>
 1     2     4
 2     3     1
 3     5     9
 4    NA    NA
 5     2     8
 6     1     5
 7    NA    NA
 8    NA    NA
 9     9     1
10     3     2

In this case, the 4th value of variable x would be filled with 5 and so on.

Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.