How to delimit the number of characters written per line in a "txt" output, in the R?

4

I am writing a filelog and want to print it on an A4 sheet. One of the parts of this filelog has a variable, with the following structure:

cod<-c(82024,82042,82067,82098,82106,82113,82141,82145,82178,82181,82184,82188,82191,82198,82212,82240,82246,82263,82280,82287,82294,82296,82298,82317,82326,82331,82336,82353,82361,82376,82382,82392,82397,82410,82425,82445,82460,82474,82476,82480,82487,82493,82533,82562,82564,82571,82578,82583,82586,82588,82590,82594,82596,82598,82610,82659,82668,82676,82678,82683)

How can I write the cod vector to a .txt file with up to 80 characters per line, assuming elements are separated by space as follows:

82487 82493 82533 82562 82564 82571 82578 82583 82586 82588 82590 82594 82596

Each line will have 13 elements (5 characters + 1 space per element).

NOTE: The limit of 80 is set according to the width of the sheet.

    
asked by anonymous 19.03.2016 / 18:38

1 answer

3

What you want can be easily achieved by using only the cat function. For example:

cat(cod, file = "jean.txt", fill = 80)

It will create the jean.txt file with the following content:

82024 82042 82067 82098 82106 82113 82141 82145 82178 82181 82184 82188 82191 
82198 82212 82240 82246 82263 82280 82287 82294 82296 82298 82317 82326 82331 
82336 82353 82361 82376 82382 82392 82397 82410 82425 82445 82460 82474 82476 
82480 82487 82493 82533 82562 82564 82571 82578 82583 82586 82588 82590 82594 
82596 82598 82610 82659 82668 82676 82678 82683

The parameter fill limits the maximum size of the line, breaking between elements of cod . If you want to separate the elements with another character, you can use sep . If you want an initial character per line (as occurs when using print ), the labels argument exists. You can also test on the console and use file only when it will save itself. For example:

> cat(cod, fill = 80, labels = paste0("[", seq(1, 1000, by = 12), "]"), sep = ", ")
[1] 82024, 82042, 82067, 82098, 82106, 82113, 82141, 82145, 82178, 82181, 
[13] 82184, 82188, 82191, 82198, 82212, 82240, 82246, 82263, 82280, 82287, 
[25] 82294, 82296, 82298, 82317, 82326, 82331, 82336, 82353, 82361, 82376, 
[37] 82382, 82392, 82397, 82410, 82425, 82445, 82460, 82474, 82476, 82480, 
[49] 82487, 82493, 82533, 82562, 82564, 82571, 82578, 82583, 82586, 82588, 
[61] 82590, 82594, 82596, 82598, 82610, 82659, 82668, 82676, 82678, 82683
    
20.03.2016 / 05:54