write.fwf {gdata}R Documentation

Write object in fixed width format

Description

write.fwf writes object in *f*ixed *w*idth *f*ormat.

Usage


write.fwf(x, file="", append=FALSE, quote=FALSE, sep=" ", na="",
  rownames=FALSE, colnames=TRUE, rowCol=NULL, justify="right",
  formatInfo=FALSE, quoteInfo=TRUE, ...)

Arguments

x data.frame or matrix, the object to be written
file character, name of file or connection, look in write.table for more
append logical, append to existing data in file
quote logical, quote data in output
na character, the string to use for missing values i.e. NA in the output
sep character, separator between columns in output
rownames logical, print row names
colnames logical, print column names
rowCol character, rownames column name
justify character, allignment of character columns
formatInfo logical, return information on number of levels, widths and format
quoteInfo logical, should formatInfo account for quotes
... further arguments to format.info, format and write.table

Details

Output is similar to print(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 "t" or similar for 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 R 2.4.0) it is not "easy" to import data back to R with 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 R if 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

Value

Besides its effect to write/export data write.fwf can provide information on format and width. A data.frame is returned with the following columns:

colname name of the column
nlevels number of unique values (unused levels of factors are dropped), 0 for numeric column
position starting column number in the output
width width of the column
digits number of digits after the decimal point
exp width of exponent in exponential representation; 0 means there is no exponential representation, while 1 represents exponent of length one i.e. 1e+6 and 2 1e+06 or 1e+16

Author(s)

Gregor Gorjanc

See Also

format.info, format, NAToUnknown, write.table, read.fwf, read.table and trim

Examples


  ## Some data
  testData <- data.frame(num1=c(1:10, NA),
                         num2=c(NA, seq(from=1, to=5.5, by=0.5)),
                         num3=c(NA, rnorm(n=10, mean=1e6, sd=3e5)),
                         int1=c(as.integer(1:4), NA, as.integer(5:10)),
                         fac1=factor(c(NA, letters[1:10])),
                         fac2=factor(c(letters[6:15], NA)),
                         cha1=c(letters[17:26], NA),
                         cha2=c(NA, letters[26:17]),
                         stringsAsFactors=FALSE)
  levels(testData$fac1) <- c(levels(testData$fac1), "unusedLevel")
  testData$Date <- as.Date("1900-1-1")
  testData$Date[2] <- NA
  testData$POSIXt <- as.POSIXct(strptime("1900-1-1 01:01:01",
                                         format="%Y-%m-%d %H:%M:%S"))
  testData$POSIXt[5] <- NA

  ## 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)

[Package gdata version 2.4.2 Index]