I have this dataframe:
df <- data.frame(id_1=c('888046309', '888046309', '888046309', '888046309', '003046309', '465798132', '465798132', '465798132', '465798132', '465798132', '465798132', '465798132', '465798132'),
id_2=c('0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309', '0003046309'))
and I would like to create a column that would indicate where a part of id_2
is present in id_1
.
I tried this from this suggestion
i <- sapply(seq_along(df$id_2), function(i) grepl(df$id_2[i], df$id_1[i]))
df$flag <- c("No", "Yes")[i + 1L]
and flag had all NO values even though you can see that 46309 is present in both id_1
& id_2
.
I then tried this from this suggestion
df$flag_2 <- str_detect(df$id_1, df$id_2)
And I got FALSEs for flag_2
Finally I tried this, and like flag_2
, I got all FALSEs for flag_3
.
df <- df %>%
mutate(flag_3 = c('No', 'Yes')[1+str_detect(id_1, as.character(id_2))])
Can these suggestions be edited so that it can indicate if some part of id_2
is present in id_1
?