R-alpha: write.table()

Kurt Hornik (Kurt.Hornik@ci.tuwien.ac.at)
Fri, 14 Mar 1997 07:33:27 +0100


Date: Fri, 14 Mar 1997 07:33:27 +0100
Message-Id: <199703140633.HAA32000@aragorn.ci.tuwien.ac.at>
From: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
To: r-testers@stat.math.ethz.ch
Subject: R-alpha: write.table()

Below are R source and documentation for a first version of
write.table().

As always, feedback is welcome.

As an aside, those who want to have their output in LaTeX format can do
something like
	write.table(data, sep = " & ", eol = " \\\\\n")
(If people kick me enough I'll perhaps add a `format' option).

Also, you'll fully appreciate the code with R version 0.50, due early
next week.

-kh

*** R source **********************************************************
write.table <- function(x, file = "", sep =" ", col.names = TRUE,
			row.names = TRUE, quote = FALSE, na = NA,
			eol = "\n")
{
  if (is.data.frame(x) && is.logical(quote) && quote)
    quote <- which(unlist(lapply(x, is.character)))
  x <- as.matrix(x)
  p <- ncol(x)
  d <- dimnames(x)
  x[is.na(x)] <- na
  if (is.logical(quote))
    quote <- if (quote && is.character(x)) 1 : p else NULL
  else if (is.numeric(quote)) {
    if (any(quote < 1 | quote > p))
      stop("invalid numbers in quote")
  }
  else
    stop("invalid quote specification")
      
  if (is.logical(row.names)) {
    if (row.names)
      x <- cbind(d[[1]], x)
  }
  else {
    row.names <- as.character(row.names)
    if (length(row.names) == nrow(x))
      x <- cbind(row.names, x)
    else
      stop("invalid row.names specification")
  }
  if (!is.null(quote) && (p < ncol(x)))
    quote <- c(0, quote) + 1

  APPEND <- FALSE
  if (is.logical(col.names))
    col.names <- if (col.names) d[[2]] else NULL
  else {
    col.names <- as.character(col.names)
    if (length(col.names) != p)
      stop("invalid col.names specification")
  }
  if (!is.null(col.names)) {
    if (!is.null(quote))
      col.names <- paste("\"", col.names, "\"", sep = "")
    cat(col.names, file = file, sep = rep(sep, p - 1))
    cat(eol, file = file, append = TRUE)
    APPEND <- TRUE
  }

  for (i in quote)
    x[, i] <- paste("\"", x[, i], "\"", sep = "")

  cat(t(x), file = file, sep = c(rep(sep, ncol(x) - 1), eol),
      append = APPEND)
}

*** Documentation ****************************************************

TITLE(write.table @@ Data Output)
USAGE(
write.table(x, file = "", sep =" ", col.names = TRUE, 
            row.names = TRUE, quote = FALSE, na = NA, 
            eol = "\n")
)
ARGUMENTS(
ARG(x @@ a data frame, matrix or vector of data.)
ARG(file @@ the name of the file which the data are to be written to.)
ARG(sep @@ the field separator string.  Values on each line of the file
are separated by this string.) 
ARG(col.names @@ a logical value indicating whether the column names of
LANG(x) are to be written along with LANG(x), or a character vector of
column names to be written.)
ARG(row.names @@ a logical value indicating whether the row names of
LANG(x) are to be written along with LANG(x), or a character vector of
row names to be written.)
ARG(quote @@ a logical or a numeric vector.  If LANG(TRUE), any strings
in the data will be surrounded by double quotes.  If a numeric vector,
its elements are taken as the indices of the variable (columns) to
quote.  In both cases, row and columns names are always quoted if they
are written.)
ARG(na @@ the string to use for missing values in the data.)
ARG(eol @@ the character(s) to print at the end of each line (row).)
)
DESCRIPTION(
LANG(write.table) converts its required argument LANG(x) to a matrix and
prints it to LANG(file).  The entries in each line (row) are separated
by the value of LANG(sep).
SEEALSO(
LANG(read.table).
)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-