There are two data sets:

x <- data.frame(c("мол", "ден", "свет")) y <- data.frame(c("молоко", "мебель", "светлый")) 

I do a cross join, I need to add a column to the result that shows whether the field from Х is contained in the field from Y.

  • What does it mean contained? Completely the same? or is there a part of the word? It is better to give the final result you want to see. - Batanichek
  • one
    From the question it is not clear why data.frame used data.frame . The solution for vectors and tables will be quite different. - Artem Klevtsov
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

According to my commentary, it’s not quite clear what you want to receive.

1) If your data.frame just text you need to add stringsAsFactors=F

 x <- data.frame (c("мол", "ден", "свет"),stringsAsFactors=F ) y <- data.frame (c("молоко", "мебель", "светлый"),stringsAsFactors=F) 

I see three possible options

- If you want to get a full match x with any y

x$x1=sapply(x[[1]],function(i) i %in%y)

- If you want to see the presence of x in any piece of the string y

x$x2=sapply(x[[1]],function(i) grepl(i,y))

- If you want to see the presence of x in y for each line x$x3=sapply(1:nrow(x),function(i) grepl(x[i,1],y[i,1]))

 > x c..мол....ден....свет.. x1 x2 x3 c..молоко....мебель....светлый.. 1 мол FALSE TRUE TRUE молоко 2 ден FALSE FALSE FALSE мебель 3 свет FALSE TRUE TRUE светлый