I want to create a dummy where all cells that include NA become 1 and all other cells -1. I tried to different things both not resulting in my desired outcome.
1) This leads to only NA Values.
Cox.Reg$FirstTask <- ifelse(Cox.Reg$active_task_avg_depth == NA, 1, -1)
2) This leads to only the -1 values being shown in the new column.
Cox.Reg$FirstTask <- ifelse(Cox.Reg$active_task_avg_depth == "NA", 1, -1)
If it is a real NA
, then we can use is.na
to detect the NA
elements, which would return TRUE
for all NA
and FALSE
for others as a logical vector, which can be used in ifelse
to change the values
ifelse(is.na(Cox.Reg$active_task_avg_depth), 1, -1)
Or another option is to create a numeric index and change the values accordingly
c(-1, 1)[is.na(Cox.Reg$active_task_avg_depth) + 1]