Splitting text to words with R and cSplit()

拜拜、爱过 提交于 2019-11-27 08:08:07

问题


I'm trying to split a series of sentences into separate words, that is to tokenize the text.

I have found an R package splitstackshape that is able to do what I want, well almost... it truncates the output to the first and last 5 rows.

Anyway, this is what I need to do:

id text
1 Lorem ipsum dolor sit amet
2 consectetur adipiscing elit
3 Donec euismod enim quis 
4 nunc fringilla sodales
5 Etiam tempor ligula vitae 
6 pellentesque dictum
7 Quisque non justo scelerisque 
8 est facilisis congue quis vel
9 Phasellus ex lorem
10 eleifend at magna vel
11 egestas eleifend massa

Output:

id word
1 Lorem
1 ipsum
1 dolor
1 sit
1 amet
2 consectetur
2 adipiscing
...

That is, I need words in separate rows, but with alongside the ID of the sentence it belongs to.

I was trying cSplit(data, "text", " ", "long"), but it truncates..


Update. FYI, here is how to do the reverse


回答1:


The cSplit function returns a data.table.

What you are describing is the default print behavior for data.tables. To see this in action, try the following:

library(data.table)
as.data.table(airquality)
print(as.data.table(airquality))

print(as.data.table(airquality), nrows = Inf)

Thus, to get the full table displayed, you can try:

library(splitstackshape)
print(cSplit(data, "text", " ", "long"), nrows = Inf)


来源:https://stackoverflow.com/questions/32624120/splitting-text-to-words-with-r-and-csplit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!