[R-gregmisc-users] SF.net SVN: r-gregmisc: [996] trunk/gdata
Brought to you by:
warnes
|
From: <gg...@us...> - 2006-10-30 19:02:47
|
Revision: 996
http://svn.sourceforge.net/r-gregmisc/?rev=996&view=rev
Author: ggorjan
Date: 2006-10-30 11:02:13 -0800 (Mon, 30 Oct 2006)
Log Message:
-----------
write.fwf
Modified Paths:
--------------
trunk/gdata/NAMESPACE
trunk/gdata/NEWS
Added Paths:
-----------
trunk/gdata/R/write.fwf.R
trunk/gdata/inst/unitTests/runit.write.fwf.R
trunk/gdata/man/write.fwf.Rd
trunk/gdata/tests/tests.write.fwf.R
trunk/gdata/tests/tests.write.fwf.Rout.save
Modified: trunk/gdata/NAMESPACE
===================================================================
--- trunk/gdata/NAMESPACE 2006-10-30 17:27:38 UTC (rev 995)
+++ trunk/gdata/NAMESPACE 2006-10-30 19:02:13 UTC (rev 996)
@@ -25,7 +25,7 @@
unmatrix,
upperTriangle,
"upperTriangle<-",
-## write.fwf,
+ write.fwf,
## mapLevels stuff
mapLevels,
@@ -85,7 +85,7 @@
S3method(isUnknown, POSIXlt)
S3method(isUnknown, list)
S3method(isUnknown, data.frame)
-## S3method(isUnknown, matrix)
+S3method(isUnknown, matrix)
S3method(unknownToNA, default)
S3method(unknownToNA, factor)
Modified: trunk/gdata/NEWS
===================================================================
--- trunk/gdata/NEWS 2006-10-30 17:27:38 UTC (rev 995)
+++ trunk/gdata/NEWS 2006-10-30 19:02:13 UTC (rev 996)
@@ -12,6 +12,10 @@
- Added "unknown" methods for matrices.
+- Added c() method for factors based on mapLevels() functions.
+
+- Added write.fwf, which writes file in *F*ixed *W*idth *f*ormat.
+
CHANGES FROM 2.1.X to 2.3.0 (2006-09-19)
---------------------------------------
Added: trunk/gdata/R/write.fwf.R
===================================================================
--- trunk/gdata/R/write.fwf.R (rev 0)
+++ trunk/gdata/R/write.fwf.R 2006-10-30 19:02:13 UTC (rev 996)
@@ -0,0 +1,104 @@
+### write.fwf.R
+###------------------------------------------------------------------------
+### What: Write fixed width format
+### $Id$
+### Time-stamp: <2006-10-30 18:58:40 ggorjan>
+###------------------------------------------------------------------------
+
+write.fwf <- function(x, file="", append=FALSE, quote=FALSE, sep=" ",
+ na="", rownames=FALSE, colnames=TRUE, rowCol=NULL,
+ justify="right", formatInfo=FALSE, quoteInfo=TRUE, ...)
+{
+ if(!(is.data.frame(x) || is.matrix(x)))
+ stop("'x' must be a data.frame or matrix")
+ if(rownames) {
+ x <- cbind(rownames(x), x)
+ rowColVal <- ifelse(!is.null(rowCol), rowCol, "row")
+ colnames(x)[1] <- rowColVal
+ }
+ colnamesMy <- colnames(x)
+
+ ## --- Format info ---
+
+ if(formatInfo) {
+ retFormat <- data.frame(colname=colnamesMy,
+ nlevels=0,
+ position=0,
+ width=0,
+ digits=0,
+ exp=0)
+ retFormat$colname <- as.character(retFormat$colname)
+
+ isNum <- sapply(x, is.numeric)
+ ## is.numeric picks also Date and POSIXt
+ isNum <- isNum & !(sapply(x, inherits, what="Date") |
+ sapply(x, inherits, what="POSIXt"))
+
+ ## Numeric is a bit special and we need to get info before format turns
+ ## all to character
+ if(any(isNum)) {
+ tmp <- lapply(x[, isNum, drop=FALSE], format.info, ...)
+ tmp1 <- sapply(tmp, length)
+ tmp <- t(as.data.frame(tmp))
+ j <- 1
+ for(i in which(isNum)) {
+ retFormat[i, 4:(3+tmp1[j])] <- tmp[j, 1:tmp1[j]]
+ ## length 1 for exp should mean 1 and not 1+1
+ if(tmp1[j] > 2 && tmp[j, 3] > 1)
+ retFormat[i, "exp"] <- retFormat[i, "exp"] + 1
+ j <- j + 1
+ }
+ }
+ }
+
+ ## --- Format ---
+
+ x <- apply(X=x, MARGIN=2,
+ FUN=function(z) {
+ ## NAToUnknown is used since format corces NA to "NA" and
+ ## then argument na in write.table does not do its job
+ format(gdata:::NAToUnknown.default(as.character(z),
+ unknown=as.character(na)),
+ justify=justify, ...) })
+ if(formatInfo) {
+ if(any(!isNum)) { # need apply as x is now a matrix
+ retFormat[!isNum, "width"] <- apply(X=x[, !isNum, drop=FALSE], MARGIN=2,
+ FUN=function(z) format.info(z)[1])
+ retFormat[!isNum, "nlevels"] <- apply(X=x[, !isNum, drop=FALSE], MARGIN=2,
+ FUN=function(z) length(unique(z)))
+ }
+ }
+
+ ## --- Write ---
+
+ if(colnames) {
+ if(rownames && is.null(rowCol)) colnamesMy <- colnamesMy[-1]
+ write.table(t(as.matrix(colnamesMy)), file=file, append=append,
+ quote=quote, sep=sep, row.names=FALSE, col.names=FALSE, ...)
+ }
+
+ write.table(x=x, file=file, append=(colnames || append), quote=quote,
+ sep=sep, row.names=FALSE, col.names=FALSE, ...)
+
+ ## --- Return format and fixed width information ---
+
+ if(formatInfo) {
+ ## be carefull with these ifelse constructs
+ retFormat$position[1] <- ifelse(quote, ifelse(quoteInfo, 1, 2), 1)
+ if(ifelse(quote, quoteInfo, FALSE)) retFormat$width <- retFormat$width + 2
+ N <- nrow(retFormat)
+ for(i in 2:N) {
+ retFormat$position[i] <- retFormat$position[i - 1] +
+ retFormat$width[i - 1] + nchar(x=sep, type="chars") +
+ ifelse(quote, ifelse(quoteInfo, 0, 1), 0)
+ }
+ if(rownames && is.null(rowCol)) {
+ retFormat <- retFormat[-1,]
+ rownames(retFormat) <- 1:(N-1)
+ }
+ return(retFormat)
+ }
+}
+
+###------------------------------------------------------------------------
+### write.fwf.R ends here
Added: trunk/gdata/inst/unitTests/runit.write.fwf.R
===================================================================
--- trunk/gdata/inst/unitTests/runit.write.fwf.R (rev 0)
+++ trunk/gdata/inst/unitTests/runit.write.fwf.R 2006-10-30 19:02:13 UTC (rev 996)
@@ -0,0 +1,97 @@
+### runit.write.fwf.R
+###------------------------------------------------------------------------
+### What: Unit tests for write.fwf
+### $Id$
+### Time-stamp: <2006-10-30 18:49:04 ggorjan>
+###------------------------------------------------------------------------
+
+### {{{ --- Test setup ---
+
+if(FALSE) {
+ library("RUnit")
+ library("gdata")
+}
+
+### }}}
+### {{{ --- write.fwf ---
+
+test.write.fwf <- function()
+{
+
+ ## 'x' must be a data.frame or matrix
+ checkException(write.fwf(1:10))
+ checkException(write.fwf(list(1:10)))
+
+ 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
+
+ ## --- output ---
+ ## in regular tests
+
+ ## --- formatInfo ---
+
+ ## default output
+ formatInfoT <- data.frame(colname=c("num1", "num2"),
+ nlevels=c(0, 0),
+ position=c(1, 4),
+ width=c(2, 3),
+ digits=c(0, 1),
+ exp=c(0, 0),
+ stringsAsFactors=FALSE)
+ formatInfo <- write.fwf(testData[, c("num1", "num2")], formatInfo=TRUE)
+ checkEquals(formatInfo, formatInfoT)
+
+ ## rowCol
+ formatInfoTR <- data.frame(colname=c("row", "num1", "num2"),
+ nlevels=c(11, 0, 0),
+ position=c(1, 4, 7),
+ width=c(2, 2, 3),
+ digits=c(0, 0, 1),
+ exp=c(0, 0, 0),
+ stringsAsFactors=FALSE)
+ formatInfoR <- write.fwf(testData[, c("num1", "num2")], formatInfo=TRUE,
+ rownames=TRUE, rowCol="row")
+ checkEquals(formatInfoR, formatInfoTR)
+
+ ## quoteInfo alone does not have any effect
+ formatInfoI <- write.fwf(testData[, c("num1", "num2")], formatInfo=TRUE,
+ quoteInfo=TRUE)
+ checkEquals(formatInfoI, formatInfoT)
+
+ ## quote
+ formatInfoQ <- write.fwf(testData[, c("num1", "num2")], formatInfo=TRUE,
+ quote=TRUE)
+ formatInfoTQ <- formatInfoT
+ formatInfoTQ$position <- c(1, 6)
+ formatInfoTQ$width <- c(4, 5)
+ checkEquals(formatInfoQ, formatInfoTQ)
+
+ ## quote without quoteInfo
+ formatInfoQI <- write.fwf(testData[, c("num1", "num2")], formatInfo=TRUE,
+ quote=TRUE, quoteInfo=FALSE)
+ formatInfoTQI <- formatInfoT
+ formatInfoTQI$position <- c(2, 6)
+ checkEquals(formatInfoQI, formatInfoTQI)
+}
+
+### }}}
+### {{{ Dear Emacs
+## Local variables:
+## folded-file: t
+## End:
+### }}}
+
+###------------------------------------------------------------------------
+### runit.write.fwf.R ends here
Added: trunk/gdata/man/write.fwf.Rd
===================================================================
--- trunk/gdata/man/write.fwf.Rd (rev 0)
+++ trunk/gdata/man/write.fwf.Rd 2006-10-30 19:02:13 UTC (rev 996)
@@ -0,0 +1,208 @@
+% write.fwf.Rd
+%--------------------------------------------------------------------------
+% What: Write fixed width format man page
+% $Id$
+% Time-stamp: <2006-10-30 20:01:16 ggorjan>
+%--------------------------------------------------------------------------
+
+\name{write.fwf}
+
+\alias{write.fwf}
+
+\concept{data output}
+\concept{data export}
+
+\title{Write object in fixed width format}
+
+\description{
+\code{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, \ldots)
+
+}
+
+\arguments{
+ \item{x}{data.frame or matrix, the object to be written}
+ \item{file}{character, name of file or connection, look in
+ \code{\link{write.table}} for more}
+ \item{append}{logical, append to existing data in \code{file}}
+ \item{quote}{logical, quote data in output}
+ \item{na}{character, the string to use for missing values
+ i.e. \code{NA} in the output}
+ \item{sep}{character, separator between columns in output}
+ \item{rownames}{logical, print row names}
+ \item{colnames}{logical, print column names}
+ \item{rowCol}{character, rownames column name}
+ \item{justify}{character, allignment of character columns}
+ \item{formatInfo}{logical, return information on number of levels,
+ widths and format}
+ \item{quoteInfo}{logical, should \code{formatInfo} account for quotes}
+ \item{\ldots}{further arguments to \code{\link{format.info}},
+ \code{\link{format}} and \code{\link{write.table}}}
+}
+
+\details{
+
+Output is similar to \code{print(x)} or \code{format(x)}. Formating is
+done completely by \code{\link{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 \code{sep}
+argument as passed to \code{\link{write.table}} via \ldots.
+
+\code{quote} can be used to quote fields in the output. Since all
+columns of \code{x} are converted to character during the output, all
+columns will be quoted! The following is needed for \code{read.fwf} or
+any other tools outside \R. If quotes are used, \code{\link{read.table}}
+can be easily used to read the data back into \R. Check examples. Do read
+details on \code{quoteInfo}.
+
+Use only *true* character i.e. not "\t" or similar for \code{sep} as
+number of characters in \code{sep} is needed internally.
+
+Use \code{na} to convert missing/unknown values. Only single value can
+be specified. Take a look at \code{\link{NAToUnknown}} if you need
+greater flexibility.
+
+If \code{rowCol} is not \code{NULL} and \code{rownames=TRUE} rownames
+will also have column name with \code{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 \code{\link{read.fwf}} if
+you also export rownames. That is the reason, that default is
+\code{rownames=FALSE}.
+
+Information about format of output can be returned if
+\code{formatInfo=TRUE}. Returned value is described in value
+section. Result is provided by \code{\link{format.info}} and care was
+taken to handle numeric properly. If output contains rownames, returned
+value accounts for this. Additionally, if \code{rowCol} is not
+\code{NULL} then returned value contains also information about format
+of rownames.
+
+If \code{quote=TRUE} output is wider due to quotes. Return value (with
+\code{formatInfo=TRUE}) can account for this in two ways; controlled
+with argument \code{quoteInfo}. However, note that there is no way to
+properly read data back to \R if \code{quote=TRUE & quoteInfo=FALSE} was
+specifed for export. \code{quoteInfo} applies only when
+\code{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).
+
+\preformatted{
+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 \code{quoteInfo=TRUE} \code{write.fwf} will return (symbolically)
+
+\preformatted{
+colname position width
+V1 1 3
+V2 5 7
+V3 13 6
+}
+
+or (with \code{quoteInfo=FALSE})
+
+\preformatted{
+colname position width
+V1 2 1
+V2 6 5
+V3 14 4
+}
+
+}
+
+\value{
+
+Besides its effect to write/export data \code{write.fwf} can provide
+information on format and width. A data.frame is returned with the
+following columns:
+ \item{colname}{name of the column}
+ \item{nlevels}{number of unique values (unused levels of factors are
+ dropped), 0 for numeric column}
+ \item{position}{starting column number in the output}
+ \item{width}{width of the column}
+ \item{digits}{number of digits after the decimal point}
+ \item{exp}{width of exponent in exponential representation; 0 means
+ there is no exponential representation, while 1 represents exponent
+ of length one i.e. \code{1e+6} and 2 \code{1e+06} or \code{1e+16}}
+}
+
+\author{Gregor Gorjanc}
+
+\seealso{
+ \code{\link{format.info}}, \code{\link{format}},
+ \code{\link{NAToUnknown}}, \code{\link{write.table}},
+ \code{\link{read.fwf}}, \code{\link{read.table}} and
+ \code{\link{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)
+ tmp <- read.fwf(file=file, widths=formatInfo$width + 1, skip=1,
+ strip.white=TRUE)
+ colnames(tmp) <- unlist(strsplit(readLines(con=file, n=1), split=" "))
+ tmp
+
+ \dontrun{
+ ## How to persuade read.fwf to accept header properly?
+ read.fwf(file=file, widths=formatInfo$width + 1, header=TRUE)
+
+ }
+
+ ## This works, but without header
+ read.fwf(file=file, widths=formatInfo$width + 1, header=FALSE, skip=1)
+
+ ## This works, but we have to use quotes
+ write.fwf(x=testData, file=file, quote=TRUE)
+ read.table(file=file, header=TRUE, strip.white=TRUE)
+
+ ## Tidy up
+ unlink(file)
+}
+
+\keyword{print}
+\keyword{file}
+
+%--------------------------------------------------------------------------
+% write.fwf.Rd ends here
Added: trunk/gdata/tests/tests.write.fwf.R
===================================================================
--- trunk/gdata/tests/tests.write.fwf.R (rev 0)
+++ trunk/gdata/tests/tests.write.fwf.R 2006-10-30 19:02:13 UTC (rev 996)
@@ -0,0 +1,61 @@
+### tests.write.fwf.R
+###------------------------------------------------------------------------
+### What: Tests for write.fwf
+### $Id$
+### Time-stamp: <2006-10-30 19:54:59 ggorjan>
+###------------------------------------------------------------------------
+
+library(gdata)
+
+## --- Test data ---
+
+num <- c(733070.345678, 1214213.78765456, 553823.798765678,
+ 1085022.8876545678, 571063.88765456, 606718.3876545678,
+ 1053686.6, 971024.187656, 631193.398765456, 879431.1)
+
+testData <- data.frame(num1=c(1:10, NA),
+ num2=c(NA, seq(from=1, to=5.5, by=0.5)),
+ num3=c(NA, num),
+ 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
+
+## --- Tests ---
+
+## Default
+write.fwf(testData)
+
+## NA should be - or ------------
+write.fwf(x=testData, na="-")
+write.fwf(x=testData, na="------------")
+
+## Some other separator than space
+write.fwf(testData[, 1:4], sep="-mySep-")
+
+## With quotes
+write.fwf(testData, quote=TRUE)
+
+## Without rownames
+write.fwf(testData, rownames=FALSE)
+
+## Without colnames
+write.fwf(testData, colnames=FALSE)
+
+## Without rownames and colnames
+write.fwf(testData, rownames=FALSE, colnames=FALSE)
+
+## With rownames and colnames and rowCol
+write.fwf(testData, rowCol="HI!")
+
+## formatInfo in unit tests
+
+###------------------------------------------------------------------------
+### tests.write.fwf.R ends
Added: trunk/gdata/tests/tests.write.fwf.Rout.save
===================================================================
--- trunk/gdata/tests/tests.write.fwf.Rout.save (rev 0)
+++ trunk/gdata/tests/tests.write.fwf.Rout.save 2006-10-30 19:02:13 UTC (rev 996)
@@ -0,0 +1,191 @@
+
+R version 2.4.0 (2006-10-03)
+Copyright (C) 2006 The R Foundation for Statistical Computing
+ISBN 3-900051-07-0
+
+R is free software and comes with ABSOLUTELY NO WARRANTY.
+You are welcome to redistribute it under certain conditions.
+Type 'license()' or 'licence()' for distribution details.
+
+ Natural language support but running in an English locale
+
+R is a collaborative project with many contributors.
+Type 'contributors()' for more information and
+'citation()' on how to cite R or R packages in publications.
+
+Type 'demo()' for some demos, 'help()' for on-line help, or
+'help.start()' for an HTML browser interface to help.
+Type 'q()' to quit R.
+
+> invisible(options(echo = TRUE))
+> ### tests.write.fwf.R
+> ###------------------------------------------------------------------------
+> ### What: Tests for write.fwf
+> ### $Id$
+> ### Time-stamp: <2006-10-30 19:54:59 ggorjan>
+> ###------------------------------------------------------------------------
+>
+> library(gdata)
+>
+> ## --- Test data ---
+>
+> num <- c(733070.345678, 1214213.78765456, 553823.798765678,
++ 1085022.8876545678, 571063.88765456, 606718.3876545678,
++ 1053686.6, 971024.187656, 631193.398765456, 879431.1)
+>
+> testData <- data.frame(num1=c(1:10, NA),
++ num2=c(NA, seq(from=1, to=5.5, by=0.5)),
++ num3=c(NA, num),
++ 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
+>
+> ## --- Tests ---
+>
+> ## Default
+> write.fwf(testData)
+num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
+ 1 1 f q 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 d j u w 1900-01-01
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ 5.5 879431.1 10 j q 1900-01-01 1900-01-01 01:01:01
+>
+> ## NA should be - or ------------
+> write.fwf(x=testData, na="-")
+num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
+ 1 - - 1 - f q - 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z - 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 - d j u w 1900-01-01 -
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ - 5.5 879431.1 10 j - - q 1900-01-01 1900-01-01 01:01:01
+> write.fwf(x=testData, na="------------")
+num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
+ 1 ------------ ------------ 1 ------------ f q ------------ 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z ------------ 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 ------------ d j u w 1900-01-01 ------------
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+ 10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+------------ 5.5 879431.1 10 j ------------ ------------ q 1900-01-01 1900-01-01 01:01:01
+>
+> ## Some other separator than space
+> write.fwf(testData[, 1:4], sep="-mySep-")
+num1-mySep-num2-mySep-num3-mySep-int1
+ 1-mySep- -mySep- -mySep- 1
+ 2-mySep- 1-mySep- 733070.345678-mySep- 2
+ 3-mySep-1.5-mySep-1214213.78765456-mySep- 3
+ 4-mySep- 2-mySep-553823.798765678-mySep- 4
+ 5-mySep-2.5-mySep-1085022.88765457-mySep-
+ 6-mySep- 3-mySep- 571063.88765456-mySep- 5
+ 7-mySep-3.5-mySep-606718.387654568-mySep- 6
+ 8-mySep- 4-mySep- 1053686.6-mySep- 7
+ 9-mySep-4.5-mySep- 971024.187656-mySep- 8
+10-mySep- 5-mySep-631193.398765456-mySep- 9
+ -mySep-5.5-mySep- 879431.1-mySep-10
+>
+> ## With quotes
+> write.fwf(testData, quote=TRUE)
+"num1" "num2" "num3" "int1" "fac1" "fac2" "cha1" "cha2" "Date" "POSIXt"
+" 1" " " " " " 1" " " "f" "q" " " "1900-01-01" "1900-01-01 01:01:01"
+" 2" "1.0" " 733070.3" " 2" "a" "g" "r" "z" " " "1900-01-01 01:01:01"
+" 3" "1.5" "1214213.8" " 3" "b" "h" "s" "y" "1900-01-01" "1900-01-01 01:01:01"
+" 4" "2.0" " 553823.8" " 4" "c" "i" "t" "x" "1900-01-01" "1900-01-01 01:01:01"
+" 5" "2.5" "1085022.9" " " "d" "j" "u" "w" "1900-01-01" " "
+" 6" "3.0" " 571063.9" " 5" "e" "k" "v" "v" "1900-01-01" "1900-01-01 01:01:01"
+" 7" "3.5" " 606718.4" " 6" "f" "l" "w" "u" "1900-01-01" "1900-01-01 01:01:01"
+" 8" "4.0" "1053686.6" " 7" "g" "m" "x" "t" "1900-01-01" "1900-01-01 01:01:01"
+" 9" "4.5" " 971024.2" " 8" "h" "n" "y" "s" "1900-01-01" "1900-01-01 01:01:01"
+"10" "5.0" " 631193.4" " 9" "i" "o" "z" "r" "1900-01-01" "1900-01-01 01:01:01"
+" " "5.5" " 879431.1" "10" "j" " " " " "q" "1900-01-01" "1900-01-01 01:01:01"
+>
+> ## Without rownames
+> write.fwf(testData, rownames=FALSE)
+num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
+ 1 1 f q 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 d j u w 1900-01-01
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ 5.5 879431.1 10 j q 1900-01-01 1900-01-01 01:01:01
+>
+> ## Without colnames
+> write.fwf(testData, colnames=FALSE)
+ 1 1 f q 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 d j u w 1900-01-01
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ 5.5 879431.1 10 j q 1900-01-01 1900-01-01 01:01:01
+>
+> ## Without rownames and colnames
+> write.fwf(testData, rownames=FALSE, colnames=FALSE)
+ 1 1 f q 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 d j u w 1900-01-01
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ 5.5 879431.1 10 j q 1900-01-01 1900-01-01 01:01:01
+>
+> ## With rownames and colnames and rowCol
+> write.fwf(testData, rowCol="HI!")
+num1 num2 num3 int1 fac1 fac2 cha1 cha2 Date POSIXt
+ 1 1 f q 1900-01-01 1900-01-01 01:01:01
+ 2 1.0 733070.3 2 a g r z 1900-01-01 01:01:01
+ 3 1.5 1214213.8 3 b h s y 1900-01-01 1900-01-01 01:01:01
+ 4 2.0 553823.8 4 c i t x 1900-01-01 1900-01-01 01:01:01
+ 5 2.5 1085022.9 d j u w 1900-01-01
+ 6 3.0 571063.9 5 e k v v 1900-01-01 1900-01-01 01:01:01
+ 7 3.5 606718.4 6 f l w u 1900-01-01 1900-01-01 01:01:01
+ 8 4.0 1053686.6 7 g m x t 1900-01-01 1900-01-01 01:01:01
+ 9 4.5 971024.2 8 h n y s 1900-01-01 1900-01-01 01:01:01
+10 5.0 631193.4 9 i o z r 1900-01-01 1900-01-01 01:01:01
+ 5.5 879431.1 10 j q 1900-01-01 1900-01-01 01:01:01
+>
+> ## formatInfo in unit tests
+>
+> ###------------------------------------------------------------------------
+> ### tests.write.fwf.R ends
+>
+> proc.time()
+[1] 1.283 0.028 1.311 0.000 0.000
+>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|