[R] how to change number of characters per line for print() to sink()?

Nevil Amos nevil.amos at gmail.com
Mon Nov 22 13:22:25 CET 2010


I am using r to read and reformat data that is then saved to a text file 
using sink(), the file has a number of initial lines of comments and 
summary data followed by print of a data.frame with no row names.
for example

a<-c(100:120)
b<-c(rnorm(100:120))
c<-c(rnorm(200:220))
mydata<-data.frame(rbind(a,b,c))
sink("datafile.txt")

cat("comments about my data \n")
cat("other calculations returned as separate text comments on a line \n")
print(mydata,row.names=F)
sink()


I need the content of the text file to keep each row of the data frame 
on a single line thus (with intervening columns present of course)

"datafile.txt"

comments about my data
other calculations returned as separate text comments on a line
           X1         X2          X3          X4          X5          
X6           
.....................................................................         
X19         X20         X21


  100.0000000 101.000000 102.0000000 103.0000000 104.0000000 
105.0000000  ......................118.0000000 119.0000000 120.0000000
   -0.3380570  -1.400905   1.0396499  -0.5802181  -0.2340614   
0.6044928   ...................................-0.4854702  -0.3677461  
-1.2033173
   -0.9002824   1.544242  -0.8668653   0.3066256   0.2490254  -1.6429223 
.....................................   0.0861146   0.4276929  -0.3408604

How doI change setting for print() or use another function to keep each 
row of the data frame as a single line ( of greater length up to approx 
300 characters) instead of wrapping the data frame into multiple lines 
of text?

The problem : I end up with the data frame split into several sections 
one under another thus

"datafile.txt"

comments about my data
other calculations returned as separate text comments on a line
           X1         X2          X3          X4          X5          X6
  100.0000000 101.000000 102.0000000 103.0000000 104.0000000 105.0000000
   -0.3380570  -1.400905   1.0396499  -0.5802181  -0.2340614   0.6044928
   -0.9002824   1.544242  -0.8668653   0.3066256   0.2490254  -1.6429223
           X7           X8          X9         X10         X11         X12
  106.0000000 107.00000000 108.0000000 109.0000000 110.0000000 111.0000000
    0.3152427   0.15093494  -0.3316172  -0.3603724  -2.0516402  -0.4556241
   -0.6502265  -0.08842649  -0.3775335  -0.4942572  -0.0976565  -0.7716651
          X13         X14         X15         X16          X17        X18
  112.0000000 113.0000000 114.0000000 115.0000000 116.00000000 117.000000
    0.8829135   0.8851043  -0.7687383  -0.9573476  -0.03041968   1.425754
    0.2666777   0.6405255   0.2342905  -0.7705545  -1.18028004   1.303601
          X19         X20         X21
  118.0000000 119.0000000 120.0000000
   -0.4854702  -0.3677461  -1.2033173
    0.0861146   0.4276929  -0.3408604



More information about the R-help mailing list