[R] placing objects with format statements into text file

Martin Maechler maechler at stat.math.ethz.ch
Thu Apr 18 08:50:05 CEST 2002

>>>>> "Patrick" == Patrick McKnight <pem at theriver.com> writes:

    Patrick> Greetings, Is there a way to explicitly state the
    Patrick> format statement for each object in a write
    Patrick> statement?  For example, I have data frame with
    Patrick> four variables (SSN that is 11 characters, and
    Patrick> three other variables with values ranging between 0
    Patrick> and 10).  I would like to have a text file written
    Patrick> that preserves a specific formatting.
    Patrick> Specifically, I would like the following result:

    Patrick> 129-02-1102 1 3 4 5
    Patrick> 123-45-345610 9 810

    Patrick> etc.

    Patrick> As you can see, the SSN takes up only 11 columns
    Patrick> and the other four variables are formated with no
    Patrick> seperator but are "put" there with the format
    Patrick> statement of f2.0.  So, is there a way to
    Patrick> accomplish this in R?  If so, I would be extremely
    Patrick> greatful for direction.

Here is an example (providing these explicitly in such questions
		   makes the question often easier to understand)

d1 <- data.frame(ssn = c("123-345-789","987-654-3"), id = 1:2, age=c(20,60))
> d1
          ssn id age
1 123-345-789  1  20
2   987-654-3  2  60

Now the idea is to use
-  write.table with  sep=""
-  format each column - coercing to character (of same width) :

> write.table(lapply(d1,format), quote=FALSE,row.names=FALSE,col.names=FALSE,sep="")

987-654-3  260


Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list