[Rd] Margins on tables

Duncan Murdoch dmurdoch at pair.com
Wed Mar 3 05:09:53 MET 2004


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



More information about the R-devel mailing list