r - 为什么 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() 函数。都返回相同的输出。发生了什么?有人可以弄清楚吗?谢谢!
解决方案
一种选择是使用递归函数将值转换为character
from factor
(整数强制值表明嵌套列表元素是factor
类),默认情况下是how = 'unlist'
in rapply
),然后我们将它们包装vector
起来list
以创建单个list
元素
list(rapply(list_tetragrams, as.character))
推荐阅读
- c# - .NET Confluent Kafka 消费者内存泄漏
- angularjs - IE11 + Angular 1.5.11 上的奇怪渲染行为
- cassandra - DataStax java.lang.AssertionError:无效的偏移量
- angular2-nativescript - 键盘上方的 Nativescript 移动按钮
- excel - 我们可以从 Excel VBA 连接到 Redshift 吗?
- powershell - 如何在 PowerShell GUI 可执行文件中嵌入图标?
- css - 如何在 Bootstrap 4 中使用 Font Awesome 中的 svg 图标?
- java - 从文件中检测空像素
- java - 我无法从 Firebase (Java) 获取特定数据
- python - 将文件搜索到目录/兼容的 Unix/Windows