如何从 tm 包的 findAssocs 函数返回的结果中删除非关联词

How to remove non associated words from results returned by findAssocs function from tm package

我正在使用 R 中 tm 包中的 findAssocs 函数来查找与给定单词集相关联的所有单词。返回的结果显示一些与任何词都没有关联的词。例如,在下面的输出中,单词 "new" 与最小相关性为 0.7 的任何单词都不相关。所以我想删除所有这些词并创建一个具有一些关联的词向量。在这种情况下,向量将为 c("blush") 。那么我怎样才能做到这一点? 谢谢

> findAssocs(myTdm,c("new","blush"),0.7)
$new
numeric(0)

$blush
  combination     customize     different       endless         flush     highlight        jdlxmd        master 
         0.98          0.98          0.98          0.98          0.98          0.98          0.98          0.98 
possibilities         three        unique           use 
         0.98          0.98          0.98          0.98  

您可以使用包 purrr 中的 compact 和函数 lengths:

findAssocsRes <-list(a=integer(0),b=c(x=1,y=2) ,c=c(z=1) )

findAssocsRes
$a
integer(0)

$b
x y 
1 2 

$c
z 
1 

purrr::compact(findAssocsRes,lengths)
$b
x y 
1 2 

$c
z 
1 

在 base R 中,您还可以将 lapplylength 一起使用:

findAssocsRes[lapply(findAssocsRes,length)>0]
$b
x y 
1 2 

$c
z 
1