[R] Variable and value labels

Viechtbauer, Wolfgang (NP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Wed Jul 12 16:28:22 CEST 2023


For value labels, factor() provides the necessary machinery. For variable labels, one could make use of comment() (something that not too many people seem to know about). To illustrate:

# look at the mtcars dataset
mtcars

# turn 'am' into a factor and use labels for the two possible levels thereof
mtcars$am <- factor(mtcars$am, levels=c(0,1), labels=c("automatic","manual"))
mtcars

# add a variable label via comment()
comment(mtcars$am) <- "Type of transmission"

# extract all the variable labels
sapply(mtcars, comment)

Best,
Wolfgang

>-----Original Message-----
>From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Anupam Tyagi
>Sent: Wednesday, 12 July, 2023 14:11
>To: Martin Maechler
>Cc: r-help mailing list
>Subject: Re: [R] Variable and value labels
>
>Thanks. Sorry, I have not consulted help pages, but did a web search. Web
>search results are referring to some package or the other for doing labels.
>Thanks for letting me know that this can be done in base-R. I will try to
>find the relevant information in R documentation.
>
>On Wed, 12 Jul, 2023, 1:33 pm Martin Maechler, <maechler using stat.math.ethz.ch>
>wrote:
>
>> >>>>> Anupam Tyagi
>> >>>>>     on Wed, 12 Jul 2023 09:18:55 +0530 writes:
>>
>>     > Hello,
>>
>>     > is there an easy way to do variable and value labels (for
>>     > factor variables) in base-R, without using a package.
>>
>> Yes, there are many.
>> How many help pages (in R , i.e. base-R)  did you consult?
>>
>> Very general questions as the above are not very useful,
>> because typically the only concised answer can be yes/no.
>>
>>
>>     > If not, what is an easy and good way to do labels, using an
>>     > add-on package.
>>
>>
>>     > --
>>     > Anupam.



More information about the R-help mailing list