[R] DISPLAYING TABLE in file
Jim Lemon
jim at bitwrit.com.au
Fri Mar 26 03:24:34 CET 2010
On 03/26/2010 11:36 AM, Geddes, Scott wrote:
>
> Hi,
>
> I wish to NEATLY store, and later display one table per file (similar to
> capabilities of write.table()).
>
> BUT BY NEATLY I MEAN:
> 1. Column Headings right aligned with data values.
> 2. Decimal points line-up per column.
> 3. Data values padded trailing zeros per column (if not integers).
> 4. "Tricky" formats such as certain characters/dates possibly chosen by
> the user?
>
> bla bla bla
>
> I do not care about increased memory storage but my main concern is to
> DISPLAY contents of file.
>
> what is best/quickest way to achieve this?
>
Hi Scott,
As Rolf mentioned, what you are trying to format is important. It does
sound like a data frame. A general solution is to create a matrix of
character strings by formatting each column of the data frame to your
liking and then writing that to a file. Thus you would:
1. left-pad the column names of the data frame with spaces using sprintf
for the column headings.
2. see 3.
3. This makes 2. easy, just pad out each column to the same number of
decimal places using the format function.
4. the format function will do a pretty good job here, sometimes formatC
will take up the slack.
However, for everyone else who might be reading this, I looked for a
decimal aligning function in R and couldn't find one. Something like this:
decimal.align<-function(x,dechar=".",ndec=NA) {
splitchar<-paste("[",dechar,"]",sep="")
splitlist<-strsplit(as.character(x),splitchar)
is(is.null(ndec))
ndec<-max(nchar(unlist(splitlist)[seq(2,2*length(x),by=2)]))
decs<-sprintf(paste("%-",ndec,"s",sep=""),
unlist(splitlist)[seq(2,2*length(x),by=2)])
return(paste(unlist(splitlist)[seq(1,2*length(x)-1,by=2)],
decs,sep="."))
}
Jim
More information about the R-help
mailing list