[R] printing name of object inside lapply

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 4 13:30:53 CEST 2008


On Thu, 4 Sep 2008, Steve Powell wrote:

> Dear list members,
> I am trying, within a lapply command, to print the name of the objects
> in list or data frame. This is so that I can use odfWeave to print out a
> report with a section for each object, including the object names.
>
> I tried e.g.
> a=b=c=1:5
> lis=data.frame(a,b,c)
> lapply(
> lis, function (z) {
> obj.nam <- deparse(substitute(z))
> cat("some other text",obj.nam,"and so on","\n")
> }
> )
>
>
> But instead of getting "a" "b" etc. I get X[[1L]] etc.
>
> Any ideas?

Use a for() loop on the names: lapply is overkill here.  But you could use

lapply(names(lis), function (z) {
    cat("some other text", z, "and so on","\n")
    ## references to lis[[z]]
})


>
>
> www.promente.org
>
> proMENTE social research
>
> Kran??evi??eva 35
> 71000 Sarajevo
>
> mob. +387 61 215 997
> tel. +387 556 865
> fax. +387 556 866
>
>
> 	[[alternative HTML version deleted]]
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list