Опция с lapply
df[paste0("matches", seq_len(nrow(df)))] <- +(do.call(cbind,
lapply(df$product, grepl, df$product)))
df
# id product matches1 matches2 matches3 matches4 matches5 matches6 matches7 matches8 matches9 matches10
#1 1 milk 1 0 0 0 0 0 0 0 0 0
#2 2 200 0 1 0 0 0 0 1 0 0 0
#3 3 gr. 0 0 1 0 0 0 0 1 0 0
#4 4 Low 0 0 0 1 0 0 0 0 0 0
#5 5 fat 0 0 0 0 1 0 0 0 0 1
#6 6 milkshake 1 0 0 0 0 1 0 0 0 0
#7 7 200 0 1 0 0 0 0 1 0 0 0
#8 8 gr. 0 0 1 0 0 0 0 1 0 0
#9 9 High 0 0 0 0 0 0 0 0 1 0
#10 10 fat 0 0 0 0 1 0 0 0 0 1
Или с использованием tidyverse
library(tidyverse)
df %>%
mutate(similar = map(product, ~
str_detect(.x, df$product) %>%
as.integer %>%
as.list %>%
set_names(str_c('matches', seq_len(nrow(df)))) %>%
as_tibble )) %>%
unnest
data
df <- structure(list(id = 1:10, product = c("milk", "200", "gr.", "Low",
"fat", "milkshake", "200", "gr.", "High", "fat")),
class = "data.frame", row.names = c(NA, -10L))