How do I program R to fill cells with missing values with data from specific cells?

I have a sample dataframe which is as follows:

structure(list(Hospital.ID1 = c(123L, 456L, NA, NA, 789L, NA, 120L,
344L, NA), Hospital.ID2 = c("", "", "ABC", "DEF", "", "GHI", "", "",
"XYZ"), Ward = c("Cardiology", "Cardiology", "Cardiology",
"Cardiology", "Cardiology", "Gastroenterology", "Gastroenterology",
"Gastroenterology", "Gastroenterology")), class = "data.frame",
row.names = c(NA,
-9L))

I would like to fill NA values in Hospital ID 1 column with values in the adjacent cell from Hospital ID 2. For instance, the first blank cell (i.e. row # 3) should be filled in as 'ABC'. Any suggestions on how I can develop a code to get the desired result?

Does the following do what you want. I had to change Hospital.ID1 to be characters to the values could be mixed with the text from Hospital.ID2.

DF <- structure(list(Hospital.ID1 = c(123L, 456L, NA, NA, 789L, NA, 120L,
                                344L, NA), 
                     Hospital.ID2 = c("", "", "ABC", "DEF", "", "GHI", 
                                      "", "","XYZ"), 
                     Ward = c("Cardiology", "Cardiology", "Cardiology",
                              "Cardiology", "Cardiology", "Gastroenterology", 
                              "Gastroenterology","Gastroenterology", "Gastroenterology")), class = "data.frame",
          row.names = c(NA,
                        -9L))
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
DF
#>   Hospital.ID1 Hospital.ID2             Ward
#> 1          123                    Cardiology
#> 2          456                    Cardiology
#> 3           NA          ABC       Cardiology
#> 4           NA          DEF       Cardiology
#> 5          789                    Cardiology
#> 6           NA          GHI Gastroenterology
#> 7          120              Gastroenterology
#> 8          344              Gastroenterology
#> 9           NA          XYZ Gastroenterology
DF <- DF |> mutate(Hospital.ID1 = as.character(Hospital.ID1), 
                   Hospital.ID1 = coalesce(Hospital.ID1, Hospital.ID2))
DF
#>   Hospital.ID1 Hospital.ID2             Ward
#> 1          123                    Cardiology
#> 2          456                    Cardiology
#> 3          ABC          ABC       Cardiology
#> 4          DEF          DEF       Cardiology
#> 5          789                    Cardiology
#> 6          GHI          GHI Gastroenterology
#> 7          120              Gastroenterology
#> 8          344              Gastroenterology
#> 9          XYZ          XYZ Gastroenterology

Created on 2024-03-03 with reprex v2.0.2

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.