[S] request for comment about "logical factors"

Dave Krantz (dhk@paradox.psych.columbia.edu)
Sun, 20 Sep 1998 17:04:39 -0400 (EDT)

In using S+ for exploratory work, I often find myself using
table() with one or several of the arguments being logical
expressions constructed on the fly, e.g.:

table(var1, var2 < 2.5, var3==4)

The table() function ends up using "FALSE" and "TRUE" to label
each dimension of the resulting array that is constructed from
a logical expression. The resulting displayed output is often
opaque--one has to think hard about what each label really means.
Lately my collaborators have been complaining about such output,
and I don't enjoy it either. It is a lot of work to edit the
output files or alternatively to set up appropriate labelled
categories or factors in advance.

As a possible remedy, I have written a small function that I
tentatively call Lf(), for "logical factor":

Lf <- function(Expr, Lab)
e <- deparse(substitute(Expr))
Lab <- paste(" (", e, ")", "==" , c("F", "T"), sep="")
factor(eval(parse(text=e)), label=Lab)

This turns the variable constructed from a logical expression
into a factor, with default labels based on the defining logical
expression, but with the option to use other labels.

Thus, the preceding call to table would be replaced by

table(var1, Lf(var2 < 2.5), Lf(var3==4))

(to get the table with default labels).

I have tried this a bit, and it seems to do what I want.
The disadvantage of typing "Lf()" repeatedly seems to me
more than compensated by escaping the choice between defining
all sorts of extra categorical or factor variables in advance
versus getting hard-to-parse output.

One obvious pitfall is that the default labels could end up
being extremely long under some circumstances.

I'm writing to solicit comments or suggestions for improvements
or other approaches to dealing with this problem.

Dave Krantz (dhk@columbia.edu)
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