Skip to content

Extract information from the names of a list within lapply

4 messages · Joshua Wiley, Henrique Dallazuanna, Jun Shen

#
Hi Jun,
On Fri, Apr 15, 2011 at 11:14 AM, Jun Shen <jun.shen.ut at gmail.com> wrote:
Not in the way you are probably trying to do it, but consider:

## 1 (what you are probably doing)
lapply(mtcars, mean)
## 2 (an alternative)
lapply(colnames(mtcars), function(x) {cat(x, fill = TRUE); mean(mtcars[, x])})

The main downside of #2 is that it takes more code, and the resulting
output is not named.  However, it gives you all the flexibility you
need.

HTH,

Josh

  
    
#
Try this:

lapply(mtcars, function(.)names(mtcars)[match.call()[[2]][[3]]])
On Fri, Apr 15, 2011 at 3:14 PM, Jun Shen <jun.shen.ut at gmail.com> wrote: