convert multiple line text to single line in R

I am trying to convert a text file with multiple lines to single line. The following is an example:

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")

I tried using the following code:

bod <- cat(str_wrap(body))

While I am getting the output in the console, the object bod is getting stored as NULL. Is it because of the length of the string?


  • Looks like you want:

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

    As documented, the result of cat is always NULL. cat's only useful for its side-effect of printing to the console device or to a file.

    To your comment-question: I'm not sure what str_wrap is. Perhaps a function from the 'stringi' or 'stringr' package. (I'm a base-R guy.)