在R中将多行文本转换为单行

时间:2015-11-08 02:47:38

标签: r string

我正在尝试将包含多行的文本文件转换为单行。以下是一个例子:

body <- c("Dear Recruiter", "Greetings from NaukriOverseasCom ", "Please find attached CVs of candidates in our database If you have an open", 
"position matching with their profile please contact himher directly", 
"This service is free however if you dont wish to receive CVs please reply", 
"with subject DND", "Sincerely", "Administrator", "NaukriOverseascom", 
"Email adminnaukrioverseascom", "Web httpwwwnaukrioverseascom", 
" httpwwwnaukrioverseascom", "Please do not print this email unless it is absolutely necessary", 
"DISCLAIMER The information contained in this electronic message and any", 
"attachments to this message are intended for the exclusive use of the", 
"addressees and may contain proprietary confidential or privileged", 
"information If you are not the intended recipient you should not", 
"disseminate distribute or copy this email Please notify the sender", 
"immediately and destroy all copies of this message and any attachments", 
"WARNING Computer viruses can be transmitted via email The recipient should", 
"check this email and any attachments for the presence of viruses The", 
"company accepts no liability for any damage caused by any virus transmitted", 
"by this email")

我尝试使用以下代码:

bod <- cat(str_wrap(body))

当我在控制台中获取输出时,对象bod将被存储为NULL。是因为字符串的长度吗?

1 个答案:

答案 0 :(得分:1)

看起来你想要:

bdy2 <- paste(body, collapse=",")

如文档所述,cat的结果始终为NULL。 cat仅对打印到控制台设备或文件的副作用有用。

对你的评论问题:我不确定str_wrap是什么。也许来自&#39; stringi&#39;或者&#39; stringr&#39;包。 (我是一名基地R家伙。)