0

I am trying to run code that creates a new variable by pasting in the value of an existing column based on whether a condition is true, using this dataset

I would like to create a new variable new_var that is set equal to Var4 and pastes in the Var3 value, if the Var4 value is NA, so it would take these values (1.8, 2.0, 3.0, 4.0, 5.0)

    df <- data.frame(var3=c(3.1, 2.0, NA, NA, 5.0),
                     var4=c(1.8, NA, 3.0, 4.0, NA))


    df$any.4[df$var4 != "NA"] <- 1
  

the any.4 flag indicates if var4 is missing or not and if any.4 = NA, then it would paste in the Var3 value. thoughts?

four77
  • 49
  • 4

0 Answers0