R, merge multiple rows of text data frame into one cell

☆樱花仙子☆ 提交于 2019-11-30 04:48:19

问题


I have a text data frame that looks like below.

> nrow(gettext.df)
[1] 3

> gettext.df
 gettext
 1 hello,
 2 Good to hear back from you.
 3 I've currently written an application and I'm happy about it

I wanted to merge this text data into one cell (to do sentiment analysis) as below

> gettext.df
  gettext
  1 hello, Good to hear back from you. I've currently written an application and I'm happy about it

so I collapsed the cell using below code

paste(gettext.df, collapse =" ")

but it seems like it makes those text data into one chunk (as one word) so I cannot scan the sentence word by word.

Is there any way that I can merge those sentence as a collection of sentences, without transforming as one big word chunk?


回答1:


You have to transform the data frame column into a character vector before using paste.

paste(unlist(gettext.df), collapse =" ")

This returns:

[1] "hello, Good to hear back from you. I've currently written an application and I'm happy about it"


来源:https://stackoverflow.com/questions/20854615/r-merge-multiple-rows-of-text-data-frame-into-one-cell

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