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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Aug 2 19:56:56 CEST 2008


You don't want format(x, nsmall=4), so why did you use it?  Try formatting 
the columns you want formatted separately.

On Sat, 2 Aug 2008, pierre8r-nabble at yahoo.fr wrote:

> Hello,
>
> How to format the output file just the way I want ?
>
> Thanks,
>
> Pierre8r
>
>
> Output file format I am looking for :
> ------------------------------------
> 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
>
>
> Output file format I get :
> --------------------------
> 2008.07.01,02:00,  1.5761,  1.5766,  1.5760,  1.5763, 65.0000
> 2008.07.01,02:15,  1.5762,  1.5765,  1.5757,  1.5761, 95.0000
> 2008.07.01,02:30,  1.5762,  1.5765,  1.5758,  1.5759, 58.0000
> 2008.07.01,02:45,  1.5758,  1.5758,  1.5745,  1.5746, 91.0000
>
>
> 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
>
> write.table(format(x,nsmall=4),
> file = "K:\\OutputFile.csv", quote=FALSE,  col.names=FALSE,
> row.names=format(index(x),"%Y.%m.%d,%H:%M"),  sep=",")

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list