2015-11-08 57 views
0

我試圖將多行文本文件轉換爲單行。以下是一個例子:將多行文本轉換爲R中的單行

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

[R,將多行文本數據幀合併到一個單元格中]可能的重複(http://stackoverflow.com/questions/20854615/r-merge-multiple-行 - 文本 - 數據 - 幀 - 到 - 單元) – 2015-11-08 03:04:46

回答

1

看起來像你想:

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

如記錄,貓的結果總是NULL。 cat僅對打印到控制檯設備或文件的副作用有用。

對你的評論問題:我不知道什麼str_wrap是。也許是'stringi'或'stringr'包中的一個函數。 (我是一個base-R的傢伙)。

+0

你是對的... str_wrap是stringr包中的一個函數。 – Apricot

+0

所以它可能會返回一個值,但不包括'cat'。 –