Sentence to Word Table with R

断了今生、忘了曾经 提交于 2019-12-21 06:01:20

问题


I have some sentences, from the sentences I want to separate the words to get row vector each. But the words are repeating to match with the largest sentence's row vector that I do not want. I want no matter how large the sentence is, the row vector of each of the sentences will only be the words one time.

sentence <- c("case sweden", "meeting minutes ht board meeting st march now also attachment added agenda today s board meeting", "draft meeting minutes board meeting final meeting minutes ht board meeting rd april")
sentence <- cbind(sentence)
word_table <- do.call(rbind, strsplit(as.character(sentence), " "))
test <- cbind(sentence, word_table)

This is what I get now,

And this is what I want,

I mean no-repeating.


回答1:


The Solution from rawr,

sentence <- c("case sweden", "meeting minutes ht board meeting st march now also attachment added agenda today s board meeting", "draft meeting minutes board meeting final meeting minutes ht board meeting rd april")
dd <- read.table(text = paste(sentence, collapse = '\n'), fill = TRUE)
test <- cbind(sentence, dd)

Or,

cc <- read.table(text = paste(gsub('\n', '', sentence), collapse = '\n'), fill = TRUE)
test1 <- cbind(sentence, cc)

Thanks.



来源:https://stackoverflow.com/questions/35855735/sentence-to-word-table-with-r

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