Skip to content
Prev 350966 / 398502 Next

print dataframe names in loop

You can automate the adding of the names to the list with the following
function, so you
can replace the
  dflist<-list(df1,df2,df3)
  names(dflist)<-c("df1","df2","df3")
with
  dflist <- namedList(df1, df2, df3)
If you supply names, such in
  dflist <- namedList(df1, Second=df2, log(df3))
it will use your names and create names for the unnamed ones.

(Once you make a named list of data.frames, you can remove the the
originals from your global environment so you will have a single version
of truth.)

namedList <- function (...)
{
    L <- list(...)
    nms <- names(L)
    if (is.null(nms)) {
        nms <- rep("", length(L))
    }
    if (any(needsName <- is.na(nms) | !nzchar(nms))) {
        nms[needsName] <- vapply(substitute(...())[needsName],
            function(x) deparse(x, nlines = 1L), FUN.VALUE = "")
        names(L) <- nms
    }
    L
}


Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, May 15, 2015 at 5:20 AM, Jim Lemon <drjimlemon at gmail.com> wrote: