[R] printing name of object inside lapply

Steve Powell steve at promente.org
Thu Sep 4 15:29:42 CEST 2008


Thanks Prof Ripley! How obvious in retrospect!

Prof Brian Ripley wrote:
> 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]]
>>
>>
>



More information about the R-help mailing list