Skip to content

new version of print.factor

2 messages · A.J. Rossini, Tony Plate

#
Thanks to Tony Plate for letting me know what the abbreviate.arg
option does.  I think this could be made more flexible (I.e. 
 =TRUE, =FALSE, =#, where # would be passed to the abbreviate
min.length argument).  But it follows the example I was given.

"print.factor" <- 
  function (x, quote = FALSE, max.levels=5, print.levels = {if
(max.levels==0) FALSE else TRUE}, abbreviate.arg=FALSE, ...)
{ 
  if (length(x) <= 0) 
    cat("factor(0)\n") 
  else print(as.character(x), quote = quote, ...) 
  if (print.levels) {
    if (abbreviate.arg) {
      cat("Levels: ", paste(abbreviate(levels(x)[1:max.levels]),
                            collapse = " "), "\n")
    } else {
      cat("Levels: ", paste(levels(x)[1:max.levels],
                            collapse = " "), "\n")
    }
  }
  invisible(x) 
} 
 

It definitely could be cleaned up a bit more.

best,
-tony
#
Here's what S-plus 6.0 does with abbreviate.arg:

# S-plus 6.0
 > print(factor(c("fooooooo","baaaaaar")))
[1] fooooooo baaaaaar
 > print(factor(c("fooooooo","baaaaaar")), quote=T)
[1] "fooooooo" "baaaaaar"
Levels:
[1] "baaaaaar" "fooooooo"
 > print(factor(c("fooooooo","baaaaaar")),abbreviate.arg=T, quote=T)
fooooooo baaaaaar
"fooo" "baar"
Levels:
[1] "baar" "fooo"

(The function below does not abbreviate vector contents when given 
abbreviate.arg==TRUE, which may be more or less suitable depending on the 
task at hand.  Also, S-plus doesn't print levels unless quote==TRUE or "If 
some of the levels in a factor object are not in represented in the 
object", but this difference was already present in the R.1.5.0 version of 
print.factor.)

-- Tony Plate
At 12:32 PM 6/19/2002 -0700, A.J. Rossini wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._