Search code examples
rdplyr

Replace a value based on a condition


I have the following dataframe

df <- data.frame(drug = c("__Drug A__", "__NA__", "__Drug B__", "__NA__", "__Drug C__", "__NA__"))

and would like to replace __NA__ values with a preciding value so that the final column will be

df <- data.frame(drug = c("__Drug A__", "__Drug A__", "__Drug B__", "__Drug B__", "__Drug C__", "__Drug C__"))

Any help will be appreciated. Thanks in advanceQ


Solution

  • You could first replace the values with NA and after that fill them down like this:

    library(dplyr)
    library(tidyr)
    df %>%
      mutate(drug = na_if(drug, "__NA__")) %>%
      fill(drug, .direction = "down")
    #>         drug
    #> 1 __Drug A__
    #> 2 __Drug A__
    #> 3 __Drug B__
    #> 4 __Drug B__
    #> 5 __Drug C__
    #> 6 __Drug C__
    

    Created on 2024-02-22 with reprex v2.0.2