Anupam,
Thanks for explaining you are talking about factors.
I see my friend Adrian has pointed out reasons you may want to use a package he
built called ?declared? but my answer will be within the regular R domain as you
asked.
You should read up a bit on factors in a book, not just blindly searching. A
factor, loosely speaking started off as a mixture of the original values as
characters and a numbered vector referencing them. When you want the original
labels, you can see them and when you want the integer indices you can see them.
> greek <- c("Alpha", "Beta", "Gamma",
"Alphabeta", "Beta", "Alpha", "Alpha
Male")
> greek
[1] "Alpha" "Beta" "Gamma"
"Alphabeta" "Beta" "Alpha"
[7] "Alpha Male">
> facgreek <- factor(greek)
> facgreek
[1] Alpha Beta Gamma Alphabeta Beta Alpha Alpha Male
Levels: Alpha Alpha Male Alphabeta Beta Gamma>
> levels(facgreek)
[1] "Alpha" "Alpha Male" "Alphabeta"
"Beta" "Gamma" > labels(facgreek)
[1] "1" "2" "3" "4" "5"
"6" "7"> str(facgreek)
Factor w/ 5 levels "Alpha","Alpha Male",..: 1 4 5 3 4 1
2> as.numeric(facgreek)
[1] 1 4 5 3 4 1 2
You can play with all kinds of things in base R such as getting the nth item as
a label, or finding all the items currently mapped to the key of 1 and so on.
> as.numeric(facgreek)
[1] 1 4 5 3 4 1 2> facgreek[5]
[1] Beta
Levels: Alpha Alpha Male Alphabeta Beta Gamma> facgreek[as.numeric(facgreek) == 1]
[1] Alpha Alpha
> as.character(facgreek)
[1] "Alpha" "Beta" "Gamma"
"Alphabeta" "Beta" "Alpha"
[7] "Alpha Male"> as.integer(facgreek)
[1] 1 4 5 3 4 1 2
Now when plotting, it depends on what you use. Base R comes with the usual plot
functions as well as others like lattice and there are packages like ggplot2.
Some of these may even convert a vector into a factor internally. In some cases,
you may want to tell it to use a factor in a certain way, such as by re-ordering
the order of the levels of a factor so the display is graphed in that order.
And as I answered another person, some graphing functions allow you to do other
kinds of labeling on top of the plot that may meet your needs.
I may be the opposite of you as I did not use R much before 2003. ?
But seriously, we often are like new users even when we once knew a bit about
something. The R from before 2003 (or was it S?) has evolved quite a bit. If,
like me, you have used lots of other computer languages in between, then they
often blend in your mind as they have overlapping paradigms and methods and of
course quirks.
And, of course, I sympathize with adjusting from other environments designed for
somewhat more specific purposes like Stata as R is more of a general purpose
programming language. Often people start with those others and then come to R
because they need to be able to do more or just fine-tune things or ?
From: Anupam Tyagi <anuptyagi at gmail.com>
Sent: Thursday, July 13, 2023 2:51 AM
To: avi.e.gross at gmail.com
Cc: r-help at r-project.org
Subject: Re: [R] Variable and value labels
Thanks, Avi. By labels I mean human readable descriptions of variables and
values of factor variables. In a plot, I want labels to be used for labelling
axes on which a factor is plotted, and variable labels for axes
names/descriptions in a plot. I may have borrowed the terminology of variable
and value labels from Stata software, which I use.
I use a lot of packages. So, I have nothing against packages. But for labelling,
I sometimes worry that I may get tied to a package for something as basic as
assigning labels, and some function/packages may not pick up the labels
correctly/well when plotting or displaying results. Maybe I am worried for
nothing.
I have not used R much after 2003. In the past few months I have begun to use R
again with R Studio, mostly for plotting and visualization of data. So, you can
think of me as a new user.
On Thu, 13 Jul 2023 at 00:14, <avi.e.gross at gmail.com
<mailto:avi.e.gross at gmail.com> > wrote:
Anupam,
Your question, even after looking at other messages, remains a bit unclear.
What do you mean by "labels"? What you mean by variables and values
and how
is that related to factors?
An example or two would be helpful so we can say more than PROBABLY.
Otherwise, you risk having many people here waste lots of time sending
answers to questions you did not ask.
And why an insistence on not using packages? If you are doing something for
a class, sure, you may need to use the basics you were supposedly taught in
class or a textbook. Otherwise, a good set of packages makes code much
easier to write and often more reliable. Realistically, quite a bit of what
some call base R is actually packages deemed useful enough to be included at
startup and that can change.
If you are new to R, note you can attach arbitrary attributes to a variable
and you can have things like named lists where some or all the items have
names as an attribute.
Factors are part of base R and are a completely different concept. You can
use base R to get or set the base levels of a factor and many other things
and there are advantages sometimes in using a vector in factor mode than
plain but also sometimes disadvantages.
If you ask a more specific and properly explained question, maybe we can
help you.
Specifically, please tell us how you plan on using your labels. As an
example, if I make a named list like this:
mylist <- list(pi=3.14, e=2.7, 666)
then I can access all elements as in mylist[[2]] without a name but
mylist$pi lets me access that item by name and mylist[["e"]] and I can
also
change the current values similarly. But without explaining what you want,
my explanation likely is not what you need.
But do note that even if you do not USE a package, you can sometimes use it
indirectly by examining the code for a function you like. If it is primarily
written in R, you may see how it does something and take a part of the code
and use it yourself.
-----Original Message-----
From: R-help <r-help-bounces at r-project.org <mailto:r-help-bounces at
r-project.org> > On Behalf Of Anupam Tyagi
Sent: Tuesday, July 11, 2023 11:49 PM
To: r-help mailing list <r-help at r-project.org <mailto:r-help at
r-project.org> >
Subject: [R] Variable and value labels
Hello,
is there an easy way to do variable and value labels (for factor variables)
in base-R, without using a package. If not, what is an easy and good way to
do labels, using an add-on package.
--
Anupam.
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org <mailto:R-help at r-project.org> mailing list --
To UNSUBSCRIBE and more, see
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.
--
Anupam.
[[alternative HTML version deleted]]