write.fwf writes object in *f*ixed *w*idth *f*ormat.write.fwf(x, file="", append=FALSE, quote=FALSE, sep=" ", na="",
rownames=FALSE, colnames=TRUE, rowCol=NULL, justify="right",
formatInfo=FALSE, quoteInfo=TRUE, ...)write.table for morefileNA in the outputformatInfo account for quotesformat.info,
format and write.tableprint(x) or format(x). Formating is
done completely by format on a column basis. Columns in
the output are by default separated with a space i.e. empty column with
a width of one character, but that can be changed with sep
argument as passed to write.table via ....quote can be used to quote fields in the output. Since all
columns of x are converted to character during the output, all
columns will be quoted! The following is needed for read.fwf or
any other tools outside R. If quotes are used, read.table
can be easily used to read the data back into R. Check examples. Do read
details on quoteInfo.
Use only *true* character i.e. not "sep as
number of characters in sep is needed internally.
Use na to convert missing/unknown values. Only single value can
be specified. Take a look at NAToUnknown if you need
greater flexibility.
If rowCol is not NULL and rownames=TRUE rownames
will also have column name with rowCol value. This is mainly for
flexibility with tools outside R. Note that (at least in R2.4.0) it
is not "easy" to import data back to Rwith read.fwf if
you also export rownames. That is the reason, that default is
rownames=FALSE.
Information about format of output can be returned if
formatInfo=TRUE. Returned value is described in value
section. Result is provided by format.info and care was
taken to handle numeric properly. If output contains rownames, returned
value accounts for this. Additionally, if rowCol is not
NULL then returned value contains also information about format
of rownames.
If quote=TRUE output is wider due to quotes. Return value (with
formatInfo=TRUE) can account for this in two ways; controlled
with argument quoteInfo. However, note that there is no way to
properly read data back to Rif quote=TRUE & quoteInfo=FALSE was
specifed for export. quoteInfo applies only when
quote=TRUE. Assume there is a file with quoted data as shown
bellow (column numbers in first three line are only for demonstration of
the values in the output).
123456789 12345678 # for position 123 1234567 123456 # for width with quoteInfo=TRUE 1 12345 1234 # for width with quoteInfo=FALSE "a" "hsgdh" " 9" " " " bb" " 123"
With quoteInfo=TRUE write.fwf will return (symbolically)
colname position width V1 1 3 V2 5 7 V3 13 6
or (with quoteInfo=FALSE)
colname position width V1 2 1 V2 6 5 V3 14 4
write.fwf can provide
information on format and width. A data.frame is returned with the
following columns:
1e+6 and 2 1e+06 or 1e+16}
[object Object]
format.info, format,
NAToUnknown, write.table,
read.fwf, read.table and
trim
## Default write.fwf(x=testData)
## NA should be - or ------------ write.fwf(x=testData, na="-") write.fwf(x=testData, na="------------")
## Some other separator than space write.fwf(x=testData[, 1:4], sep="-mySep-")
## Write to file and report format and fixed width information file <- tempfile("test.txt") formatInfo <- write.fwf(x=testData, file=file, formatInfo=TRUE)
## Read exported data back to R (note +1 due to separator) ## ... without header read.fwf(file=file, widths=formatInfo$width + 1, header=FALSE, skip=1, strip.white=TRUE)
## ... with header - via postimport modfication tmp <- read.fwf(file=file, widths=formatInfo$width + 1, skip=1, strip.white=TRUE) colnames(tmp) <- read.table(file=file, nrow=1, as.is=TRUE) tmp
## ... with header - persuading read.fwf to accept header properly ## (thanks to Marc Schwartz) read.fwf(file=file, widths=formatInfo$width + 1, strip.white=TRUE, skip=1, col.names=read.table(file=file, nrow=1, as.is=TRUE))
## ... with header - with the use of quotes write.fwf(x=testData, file=file, quote=TRUE) read.table(file=file, header=TRUE, strip.white=TRUE)
## Tidy up unlink(file)