首页 > 解决方案 > 将不同长度的字符串列表组合到一个数据框中

问题描述

我有一个需要纠正英语错误的文本数据。

我想要一个表格的输出,第一列是错误,第二列是所有更正的建议。

例如:

sentence <- "This is a word but thhis isn't and this onne as well. I need hellp"

library(hunspell)
mistakesList <- hunspell(essay)[[1]]
suggestionsList <- hunspell_suggest(mistakesList)

我试过了

do.call(rbind, Map(data.frame, A=mistakesList, B=suggestionsList))

但它返回

            A      B
thhis   thhis   this
onne.1   onne   none
onne.2   onne    one
onne.3   onne  tonne
onne.4   onne  Donne
onne.5   onne   once
onne.6   onne   Anne
onne.7   onne Yvonne
hellp.1 hellp  hello
hellp.2 hellp   hell
hellp.3 hellp   help
hellp.4 hellp hell p

我想要一个返回的数据框:

mistakes suggestions
thhis   this
onne    none one tonne Donne once Anne Yvonne
hellp   hello hell help hell p

标签: rtextmininghunspell

解决方案


我们可以保持mistakesList原样并suggestionsList使用toString.

data.frame(mistakes = mistakesList, suggestions = sapply(suggestionsList, toString))


#  mistakes                               suggestions
#1    thhis                                      this
#2     onne none, one, tonne, Donne, once, Anne, neon
#3    hellp                 hello, hell, help, hell p

推荐阅读