[R] How to format the output file just the way I want ?

Pierre8rou pierre8r-nabble at yahoo.fr
Mon Aug 4 11:40:44 CEST 2008


I think I have found the answer myself.
If you have something better, please write it.

Pierre8r

My R code :
-----------


library(quantmod)
library(xts)

Lines <-
"2008.07.01,02:00,1.5761,1.5766,1.5760,1.5763,65
2008.07.01,02:15,1.5762,1.5765,1.5757,1.5761,95
2008.07.01,02:30,1.5762,1.5765,1.5758,1.5759,58
2008.07.01,02:45,1.5758,1.5758,1.5745,1.5746,91
2008.07.01,03:00,1.5745,1.5751,1.5744,1.5744,117
2008.07.01,03:15,1.5742,1.5742,1.5727,1.5729,100
2008.07.01,03:30,1.5730,1.5736,1.5730,1.5736,61
2008.07.01,03:45,1.5735,1.5740,1.5735,1.5739,55"

quotes <- read.csv(textConnection(Lines), header=FALSE)
x <- as.xts(quotes[,-(1:2)],
as.POSIXct(paste(quotes[,1],quotes[,2]),format='%Y.%m.%d %H:%M'))
colnames(x) <- c('Open','High','Low','Close','Volume')

x$Open <- sprintf("%5.04f", x$Open)
x$High <- sprintf("%5.04f", x$High)
x$Low <- sprintf("%5.04f", x$Low)
x$Close <- sprintf("%5.04f", x$Close)

x$Volume <- sprintf("%.0f", x$Volume)

write.table(x,file = "K:\\OutputFile.csv", quote=FALSE,  col.names=FALSE,
row.names=format(index(x),"%Y.%m.%d,%H:%M"),  sep=",")


-- 
View this message in context: http://www.nabble.com/How-to-format-the-output-file-just-the-way-I-want---tp18790926p18808276.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list