Skip to content
Prev 257002 / 398506 Next

Extract information from the names of a list within lapply

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