[R] Creating Hmisc labels inside a function

Frank E Harrell Jr f.harrell at vanderbilt.edu
Wed Sep 19 15:08:00 CEST 2007


Steve Powell wrote:
> Thanks again Frank for quick reply.
> True, 
> 
> someobject=2
> Test=function(obj,labe)
>    {
>    label(obj)=labe
>    #at this point add the line:
>    obj
>    }
>  Test(someobject,"somelabel") 
> #returns a label. But if you retype 
> someobject
> #the label has gone. That is what I meant by the label not being permanently
> changed.

Why would that work?  someobject was never changed in any way.  Use 
someobject <- Test(someobject, 'new label') or if that's all you want, 
just label(someobject) <- 'new label'

Frank

> Steve 
>  
> proMENTE social research 
> research | evaluation | training & consulting 
> Kranjčevićeva 35, 71000 Sarajevo 
> mobile: +387 61 215 997 | office: +387 33 556 865 | fax: +387 33 556 866
> skype: stevepowell99 
> www.promente.org  |  www.mojakarijera.com  |  www.psih.org  
> 
> -----Original Message-----
> From: Frank E Harrell Jr [mailto:f.harrell at vanderbilt.edu] 
> Sent: 17 September 2007 17:18
> To: Steve Powell
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Creating Hmisc labels inside a function
> 
> Steve Powell wrote:
>>  
>> Thanks, Frank - it doesn't work though. 
>> Your suggestion was:
>>
> Test=function(obj,labe)
>   {
>   label(obj)=labe
>   #at this point add the line:
>   obj
>   }
>> #...but just returning the object does not permanently change the label:
>> obj
> 
> It does for me.  Please re-run.  -Frank
> 
>  > attributes(Test(1:3, 'some label'))
> $label
> [1] "some label"
> 
> $class
> [1] "labelled"
> 
> You don't need assign.
> Frank
> 
> 
>> That is why I wanted to use assign. But "assign" will not work with 
>> attributes, labels etc. So
>>
>> assign(label(obj),"some label") #doesn't even work outside a function, 
>> at the command prompt.
>>
>> Any more ideas anyone?
>> Best wishes
>> Steve Powell
>>
>>  
>> proMENTE social research
>> research | evaluation | training & consulting Kranjčevićeva 35, 71000 
>> Sarajevo
>> mobile: +387 61 215 997 | office: +387 33 556 865 | fax: +387 33 556 
>> 866
>> skype: stevepowell99
>> www.promente.org  |  www.mojakarijera.com  |  www.psih.org
>>
>> -----Original Message-----
>> From: Frank E Harrell Jr [mailto:f.harrell at vanderbilt.edu]
>> Sent: 12 September 2007 14:38
>> To: Steve Powell
>> Cc: r-help at stat.math.ethz.ch
>> Subject: Re: [R] Creating Hmisc labels inside a function
>>
>> Steve Powell wrote:
>>> hello list members,
>>> I am wanting to write a label using the Hmisc package, but inside a 
>>> function. Normally I can do:
>>>
>>> library(Hmisc)
>>> M=2
>>> label(M)="lab"
>>>
>>> #But inside a function the "=" does not work:
>>> Test=function(obj,labe)
>>> {
>>> label(obj)=labe
>> at this point add the line:
>>
>> obj
>>
>>> }
>> The returned object will have what you need.  -Frank
>>
>>> Test(M,"new label")
>>>
>>> I usually use the "assign" function to make assignments inside 
>>> functions, but assign will not work with attributes, labels etc.
>>> Any ideas?
>>> Thanks in advance
>>>
>>> Steve Powell
>>>
>>>  
>>> proMENTE social research
>>> research | evaluation | training & consulting Kranjčevićeva 35, 71000 
>>> Sarajevo
>>> mobile: +387 61 215 997 | office: +387 33 556 865 | fax: +387 33 556
>>> 866
>>> skype: stevepowell99
>>> www.promente.org  |  www.mojakarijera.com  |  www.psih.org
>>>
>>>
>>> Checked by AVG Free Edition. 
>>>
>>> 17:43
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
> 
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University



More information about the R-help mailing list