Patrick McKnight
2002-Apr-17 22:26 UTC
[R] placing objects with format statements into text file
Greetings, Is there a way to explicitly state the format statement for each object in a write statement? For example, I have data frame with four variables (SSN that is 11 characters, and three other variables with values ranging between 0 and 10). I would like to have a text file written that preserves a specific formatting. Specifically, I would like the following result: 129-02-1102 1 3 4 5 123-45-345610 9 810 etc. As you can see, the SSN takes up only 11 columns and the other four variables are formated with no seperator but are "put" there with the format statement of f2.0. So, is there a way to accomplish this in R? If so, I would be extremely greatful for direction. -- Cheers, Patrick -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2002-Apr-18 06:50 UTC
[R] placing objects with format statements into text file
>>>>> "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))> d1ssn 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="")123-345-789120 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Patrick McKnight
2002-Apr-18 17:49 UTC
[R] placing objects with format statements into text file
Martin, Thanks for your help. It worked flawlessly! The lapply(d1,format) was the part that I was missing. Thanks again. Cheers, Patrick On Thu, 18 Apr 2002, Martin Maechler wrote:> >>>>> "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="") > > 123-345-789120 > 987-654-3 260 > > ---- >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._