What am I doing wrong below? I'm trying to create a new column which replaces the values of Col_1 with the value in the matching row of Col_2 IF it matches a certain string, ELSE it returns the values of Col_1.
Many examples exist - mainly this and this but they seem to operate on the 1st column in a dataframe - for which the code below works fine. I have deliberately slotted in a 1st column to reflect the needs of my data:
What I have tried
library(dplyr)
df <- data.frame(cbind('num' = c(1,2,3,4),
'tenure' = c('Unknown', 'a', 'Unknown', 'b'),
'tenure_2' = c('t', 's', 'u', 'v')))
df['new_col'] <- df %>%
mutate(tenure = ifelse(tenure == "Unknown", tenure_2, tenure))
Why does it select the 1st col? I'm more than happy to hear better solutions I may have overlooked.