pierre8r-nabble at yahoo.fr
2008-Aug-02 16:52 UTC
[R] How to format the output file just the way I want ?
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=",") ____________________________________________________________
Prof Brian Ripley
2008-Aug-02 17:56 UTC
[R] How to format the output file just the way I want ?
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
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.