markleeds at verizon.net
2006-Jul-04 15:03 UTC
[R] [Fwd: formatting using the write statement]
>I have a series of write statements because >i am writing to a file >where the characters strings are the column names of a dataframe >and the numbers are the elements in a particular row. >So, a file might look like > >AAA 2.1 >BB 3.1 >AHLZ 0.2 > >and it would be named "rowname".mls. > >so, each time i get to a new row, i create a new file and write to it. > >the code is below and it works fine. my only question is >there a way to tell the write statement that i want the data >to be written in a fixed format ( same number of spaces >betweewn character strings and numbers ) rather than written as it >is above ? i tried sep = " ", instead of ncolumns butt hat >just makde things worse. thanks. > >for (rownum in 1 (1:nrow(sortedForecastData)) { > >outfile=paste(forecsastfiledir,rownames(sortedForecastData[rownum],".mls",sep="") > >for colnum in (1:ncol(sortedForecastData) { > >if ( colnum == 1 ) { > >write(c(colnames(sortedForecastData[colnum],sortedForecastData[rownum,colnum],file=outfile,ncolumns=2,append=FALSE) > >} else { > >write(c(colnames(sortedForecastData)[colnum],sortedForecastData[rownum,colnum],file=outfile,ncolumns=2,append=TRUE ) > >} >}
You can use sprintf to do the fixed field spacing. This example has 10 columns for the rowname, left justified: outFiles <- paste(rownames(DF), '.mls', sep='') # create all file names at once for (i in seq(nrow(DF))){ # use sprintf to do the fixed spacing and all columns at once # 'cat' writes out the file cat(sprintf("%-10s %.1f\n", colnames(DF), DF[i,]), sep='', file=outFiles[i]) } On 7/4/06, markleeds@verizon.net <markleeds@verizon.net> wrote:> > > >I have a series of write statements because > >i am writing to a file > >where the characters strings are the column names of a dataframe > >and the numbers are the elements in a particular row. > >So, a file might look like > > > >AAA 2.1 > >BB 3.1 > >AHLZ 0.2 > > > >and it would be named "rowname".mls. > > > >so, each time i get to a new row, i create a new file and write to it. > > > >the code is below and it works fine. my only question is > >there a way to tell the write statement that i want the data > >to be written in a fixed format ( same number of spaces > >betweewn character strings and numbers ) rather than written as it > >is above ? i tried sep = " ", instead of ncolumns butt hat > >just makde things worse. thanks. > > > >for (rownum in 1 (1:nrow(sortedForecastData)) { > > > > >outfile=paste(forecsastfiledir,rownames(sortedForecastData[rownum],".mls",sep="") > > > >for colnum in (1:ncol(sortedForecastData) { > > > >if ( colnum == 1 ) { > > > > >write(c(colnames(sortedForecastData[colnum],sortedForecastData[rownum,colnum],file=outfile,ncolumns=2,append=FALSE) > > > >} else { > > > >write(c(colnames(sortedForecastData)[colnum],sortedForecastData[rownum,colnum],file=outfile,ncolumns=2,append=TRUE > ) > > > >} > >} > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]