I have a large data frame that looks like this
library(tidyverse)
df <- tibble(fruits=c("apple","banana","ananas"),
col1=c(4,NA,NA),
col2=c(5,NA,10),
col3=c(10,100,5),
col4=c(20,4,1))
df
#> # A tibble: 3 × 5
#> fruits col1 col2 col3 col4
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 apple 4 5 10 20
#> 2 banana NA NA 100 4
#> 3 ananas NA 10 5 1
Created on 2022-03-13 by the reprex package (v2.0.1)
When there is an NA value only of the col1, I want to fill the NA with the last non-NA value of the row and return the value to NA.
I want my data to look like this.
#> # A tibble: 3 × 5
#> fruits col1 col2 col3 col4
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 apple 4 5 10 20
#> 2 banana 100 NA NA 4
#> 3 ananas 10 NA 5 1