[R] data file - function write.fwf - library gdata

Paulo Barata pbarata at infolink.com.br
Tue Feb 24 14:53:47 CET 2009


Dear R-list members,

I have a data file with thousands of lines (cases), where each line
contains the values of several variables. I would like to separate
these lines in small groups, with each group followed by a blank
line, to ease the visual inspection of the data in some situations.

I am writing the output files with function write.fwf in library
gdata, for correct column alignment.

Below is a small-scale example, which requires library gdata.
The results are shown just below the code.

In the example, output file 1 gives the correct result I am looking
for. But the real production code should be, I think, something
like the one that produces output file 2. But that code is not
working properly.

My questions are: in output file 2, why data that should
belong to the same line were written out in different lines? Why
wasn't each line written out a whole by the inner for command? What
causes the splitting of each line? How to go around this problem?

I am using R 2.8.1 running on Windows XP.

###-----------------------------------------------------------------------

### small-scale example

file.1 <- 'test-1.txt'
file.2 <- 'test-2.txt'

### this is just to construct a small dataframe x
a <- c(1,2,3,4,5,6)
b <- c(111,222,333,444,555,666)
x <- data.frame(a,b)
names(x) <- c('aaa','bbb')

space <- data.frame(' ')

library(gdata)

### build output file 'test-1.txt'
write.fwf(x[0,],file=file.1)                              # just the 
header
write.fwf(space,file=file.1,colnames=FALSE,append=TRUE)   # a blank line
write.fwf(x[1:3,],file=file.1,colnames=FALSE,append=TRUE) # two lines
write.fwf(space,file=file.1,colnames=FALSE,append=TRUE)   # a blank line
write.fwf(x[4:6,],file=file.1,colnames=FALSE,append=TRUE) # two lines

### build output file 'test-2.txt'
write.fwf(x[0,],file=file.2)                              # just the 
header
write.fwf(space,file=file.2,colnames=FALSE,append=TRUE)   # a blank line
for (k in 1:2) {                                          # two groups
  for (j in 1:3) {                                         # with 
three lines each
    write.fwf(x[3*(k-1)+j,],file=file.2,colnames=FALSE,append=TRUE)
  } # for j
  write.fwf(space,file=file.2,colnames=FALSE,append=TRUE)  # a blank 
line
} # for k

###-----------------------------------------------------------------------

These are the results:

Output file test-1.txt (the correct results):

aaa bbb

1 111
2 222
3 333

4 444
5 555
6 666

-----------

Output file test-2.txt (each line has been split in two lines):

aaa bbb

1
111
2
222
3
333

4
444
5
555
6
666

-----------

Thank you very much.

Paulo Barata

--------------------------------------------------------------------
Paulo Barata
Fundacao Oswaldo Cruz - Oswaldo Cruz Foundation
Rua Leopoldo Bulhoes 1480 - 8A
21041-210  Rio de Janeiro - RJ
Brazil

E-mail: pbarata at infolink.com.br
Alternative e-mail: paulo.barata at ensp.fiocruz.br




More information about the R-help mailing list