[S] Dates in a data frame

Therneau, Terry M., Ph.D. (therneau@mayo.edu)
Fri, 3 Apr 1998 07:21:23 -0600


We had the same problem with the display of dates in a data frame, as
well as survival objects and other classes with special "printout"
methods. Our solution has 3 parts:

1. Replace the print.data.frame function. Here is the default version

function(x, ..., quote = F, right = T)
{
if(!length(x))
cat("NULL data frame with", length(row.names(x)), "rows\n")
else if(!length(row.names(x))) {
print.atomic(names(x), quote = F)
cat("< 0 rows> \n")
}
else print(as.matrix(x), ..., quote = quote, right = right)
invisible(x)
}

The key idea is to make "as.matrix" return a character matrix (it is
going to be printed, after all). To do this

a. Change "else print(as.matrix(x), ......." to
"else { x <- as.matrix(x)
print(x, ..., quote = quote, right = right)
}
b. Now replace the call to 'as.matrix' with a copy of the
as.matrix.data.frame function. This was just cut-and-paste with an
editor.

c. Add 1 line to the new code. Change

xj <- X[[j]]
to
xj <- X[[j]]
if (!is.null(class(xj)) xj <- X[[j]] <- format(xj, ...)

2. Make sure that a format method exists for whatever classes you
desire (already exists for 'chron' objects).

3. We made this function a system-wide override to the standard
print.data.frame function.

Terry Therneau
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news