mutate data frame if cells are empty

I have a data frame like below

df <- data.frame(HOD_ID =c("","KTN2252","ANA2719","ITI2624","DEV2698","HRT2921","","KTN2624","ANA2548","ITI2535","DEV2732","HRT2837","ERV2951","KTN2542","ANA2813","ITI2210"),
                 DEPT_Head = c("DEV0001","KTN2252","ANA2719","ITI2624","DEV2698","HRT2921","ERV0000","KTN2624","ANA2548","ITI2535","DEV2732","HRT2837","ERV2951","KTN2542","ANA2813","ITI2210"))

my code always required input parameters
DEPT_Head = "DEV0001"

now i want to mutate my data frame if HOD_ID for user input Dept_head should be blank , and if there is any other blank cells except user input parameters the mutate new column to 1
HOD_ID should always be blank for user defined DEPT_Head , else if there any blank cells then mutate if_blank to 1

the output should be like

HOD_id Dept_id if_blank
DEV0001 0
KTN2252 KTN2252 0
ANA2719 ANA2719 0
ITI2624 ITI2624 0
DEV2698 DEV2698 0
HRT2921 HRT2921 0
ERV0000 1
KTN2624 KTN2624 0
ANA2548 ANA2548 0
ITI2535 ITI2535 0
DEV2732 DEV2732 0
HRT2837 HRT2837 0
ERV2951 ERV2951 0
KTN2542 KTN2542 0
ANA2813 ANA2813 0
ITI2210 ITI2210 0

You can do this with if_else which takes a logical condition and the values to return if the condition is true or false

df %>% 
  mutate(if_blank = if_else(
    condition = HOD_ID == "" & DEPT_Head != "DEV0001", 
    true = 1, 
    false = 0))

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