Replacing blank/NA values with values from another column

Hi. In the dataset below, I need to fill the student ID where missing with the new ID. There must not be NAs in the student ID column. How can I do this?

library(tidyverse)

sample<-tibble::tribble(
  ~student.id,  ~new.id,
       "S001",       NA,
       "S002",       NA,
       "S003",       NA,
       "S004",       NA,
           NA, "BEN005",
       "S065",       NA,
       "S066",       NA,
           NA, "BEN006",
       "S085",       NA,
       "S086",       NA,
       "S087",       NA
  )

dplyr has coalesce

sample <- sample %>% mutate(student.id = coalesce(student.id, new.id)

Thanks a lot.

Regards,
Nithin

This topic was automatically closed 7 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.