[R] How to assess object names within a function in lapply or l_ply?

hadley wickham h.wickham at gmail.com
Mon Sep 28 16:27:40 CEST 2009


> or with l_ply (plyr package)
> l_ply(data.frame(a=1:3, b=2:4), function(x) print(deparse(substitute(x))))


The best way to do this is to supply both the object you want to
iterate over, and its names.  Unfortunately it's slightly difficult to
create a data structure of the correct form to do this with m_ply.

df <- data.frame(a=1:3, b=2:4)
input <- list(x = df, name = names(df))
inputdf <- structure(input,
    class = "data.frame",
    row.names = seq_along(input[[1]]))

m_ply(inputdf, function(x, name) {
  cat(name, "---------\n")
  print(x)
})

I'll think about how to improve this for a future version.

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list