在两个列表中查找常用词

时间:2018-02-28 01:08:41

标签: r list text-mining intersect

x1 <- c("I like apple", "she enjoys reading")
x2 <- c("he likes apple", "Mike wants to read this book")  
w1 <- strsplit(x1, " ")
w2 <- strsplit(x2, " ")  

我得到两个名单:

w1  
[[1]]  
[1] "I"      "like"   "apple"  

[[2]]  
[1] "she"      "enjoys" "reading"

w2  
[[1]]  
[1] "he"    "likes"  "apple"

[[2]]  
[1] "Mike"  "wants" "to"    "read"  "this"  "book"  

我想要

intersect(w1[[1]], w2[[1]])
intersect(w1[[2]], w2[[2]])

假设w1w2的长度非常大,因此使用for循环不是一种有效的方法。有没有更方便的方法来获得相应的交叉?

1 个答案:

答案 0 :(得分:3)

我们可以使用Map来应用相应元素上的函数

Map(intersect, w1, w2)
#[[1]]
#[1] "apple"

#[[2]]
# character(0)

或使用pmap/map2

中的purrr
library(purrr)
pmap(list(w1, w2), intersect)
相关问题