0

basically I have a bunch of text documents that have been read in via readLines. I then want to collapse each element as a new line of a document. The paste(files,collapse=" ") can dot his but spaces are used.

Unfortunately using:

parsed <- lapply(files, function(x) paste(x,collapse="\n"))

embeds a literal \n in the collapsed document. So the collapse parameter will not except special characters such as newline, tab and so on. It is entirely hard coded.

However using cat gets rid of this behaviour:

parsed <- lapply(files, function(x) cat(paste(x,collapse="\n")))

but the output is sent to the console (ie. all files). The output is correct but it is not being stored in files

Have I missed something, or does the nature of cat not lend itself to storing output into variables?

brucezepplin
  • 9,202
  • 26
  • 76
  • 129
  • 2
    There's not a literal "`\n`" in your string, that's just how R prints new lines to the console. Do not use `cat` in the `lapply` function. You can use it on `cat(parsed)` after the fact if you want to print the new lines as new lines. – MrFlick May 18 '16 at 14:08
  • thanks very much. I also had to `unlist(parsed)` to pass into `cat` – brucezepplin May 18 '16 at 14:12
  • Then you might want to be using `sapply()` rather than `lapply()` – MrFlick May 18 '16 at 14:13

0 Answers0