首页 > 解决方案 > 为什么 unlist() 将字符串列表转换为数字?

问题描述

我在 R 中进行文本分析。我有一个包含 ngram 的列表列表。

看起来像这样:

> list_tetragrams[459]
[[1]]
 [1] a small stage show          album of jazz standards     an album of jazz            and play small rooms       
 [5] and release an album        can translate into a        her late s and              i think she’ll wait        
 [9] in her late s               into a small stage          late s and release          maybe something she can    
[13] one can dream right         play small rooms jazz       release an album of         s and release an           
[17] she can translate into      she’ll wait until she’s     she’s in her late           show and play small        

我想将此列表列表转换为一个列表。这是我所做的和输出:

Fngram<- list(unlist(unlist(list_tetragrams)))

Output:
 [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  1  2  3  1  1  1  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
  [39] 20 21 22 23 24 25 26 27 28 29 30 31 32 33  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

代码我用过多次,第一次出现这样的情况。我曾尝试使用 flatten() 函数或 do.all() 函数。都返回相同的输出。发生了什么?有人可以弄清楚吗?谢谢!

标签: rlisttextnlp

解决方案


一种选择是使用递归函数将值转换为characterfrom factor(整数强制值表明嵌套列表元素是factor类),默认情况下是how = 'unlist'in rapply),然后我们将它们包装vector起来list以创建单个list元素

list(rapply(list_tetragrams, as.character)) 

推荐阅读