[Solved] how to compare words and find match words in R


I don’t think data.frame() can handle vectors as individual elements, so I used data_frame() from the tibble package:

df <- tibble::data_frame(var1 = list(c("apple", "www"), c("dog", "cat", "kkk")), var2 = list(c("apple", "zzz"), c("cat", "kkk")))

apply function by row, where the function takes the intersection of the first and second list elements:

apply(df, 1, function(x) intersect(x[[1]], x[[2]]))
[[1]]
[1] "apple"

[[2]]
[1] "cat" "kkk"

0

solved how to compare words and find match words in R