From dmurdoch at pair.com  Wed Mar  3 05:09:53 2004
From: dmurdoch at pair.com (Duncan Murdoch)
Date: Wed Mar  3 05:09:48 2004
Subject: [Rd] Margins on tables
In-Reply-To: <0ABD88905D18E347874E0FB71C0B29E90179DE73@exdkba022.novo.dk>
References: <0ABD88905D18E347874E0FB71C0B29E90179DE73@exdkba022.novo.dk>
Message-ID: <mhka40d4ph7aca2le6h4o0gcf7l7plqpkf@4ax.com>

On Tue, 2 Mar 2004 16:56:58 +0100, you wrote:

>I wanted to do this, but in R when you have a single object as argument
>you can
>retrieve its name by the deparse(substitute())-trick, but if the
>function is just
>given as part of a list element there is no way (thai I know of) to get
>at its 
>name. You can of course do the deparse(substitute())-trick, and embark
>on major 
>text-processing to get you the name.

I was just thinking of the first case, where you had something like

margins(A, FUN=mean)

but I think you can handle the more complicated case using
substitute(FUN), i.e. and using this function, which recursively goes
through a list, and adds a name matching the value when the value is
simple.

add.names <- function(thelist) {
    n <- names(thelist)
    if (is.null(n)) n <- rep("", length(thelist))
    for (i in seq(along=thelist)[-1]) {
        if (!is.call(thelist[[i]])) {
            if (n[i] == "") n[i] <- as.character(thelist[[i]])
        } else if (as.character(thelist[[i]][[1]]) == "list") 
                thelist[[i]] <- add.names(thelist[[i]])
    }
    names(thelist) <- n
    thelist
}

I will add your function to the new "stats" package which is in the
base R distribution (the same package holding ftable).  I think it
would be better to name it "add.margins", since "margins" sounds like
a function to extract margins.  What do you think of that change?

Duncan Murdoch


[Rd] Margins on tables
Duncan Murdoch