Re: [R] Variable and value labels

2023-07-13 Thread Anupam Tyagi
Thanks again, Avi. I will read up more on factors.

On Thu, 13 Jul, 2023, 8:51 pm ,  wrote:

> 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 
> *Sent:* Thursday, July 13, 2023 2:51 AM
> *To:* avi.e.gr...@gmail.com
> *Cc:* r-help@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,  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 

Re: [R] Variable and value labels

2023-07-13 Thread avi.e.gross
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  
Sent: Thursday, July 13, 2023 2:51 AM
To: avi.e.gr...@gmail.com
Cc: r-help@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, mailto:avi.e.gr...@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 

Re: [R] ggplot: Can plot graphs with points, can't plot graph with points and line

2023-07-13 Thread John Kane
Hi John,

This should do what you want.  I've changed your data.frame name for my own
convenience to "dat1".

###===
dat1  <- data.frame(
Time  = c("Age.25","Age.35","Age.45","Age.55"),
Medians = c(128.25,148.75,158.5,168.75)
)

# create segments data.frame

dat2  <- data.frame(x = dat1$Time[1:3], xend = dat1$Time[2:4],
y = dat1$Medians[1:3], yend = dat1$Medians[2:4])



p1  <- ggplot(dat1 ,aes(x = Time, y = Medians)) +
  geom_point()

p1 + geom_segment( x = "Age.25", y = 128.25, xend = "Age.35", yend =
148.75) +
  geom_segment( x = "Age.35", y = 148.75, xend = "Age.45", yend = 158.5) +
  geom_segment( x = "Age.45", y = 158.5, xend = "Age.55", yend = 168.55)


# This solution shamelessly stolen from
##
https://stackoverflow.com/questions/62536499/how-to-draw-multiple-line-segment-in-ggplot
p1 + geom_segment(
  data = dat2,
  mapping = aes(x=x, y=y, xend=xend, yend=yend),
  inherit.aes = FALSE
)


On Thu, 13 Jul 2023 at 01:11, Jim Lemon  wrote:

> Hi John,
> I'm not sure how to do this with ggplot, but:
>
> Time<- c("Age.25","Age.35","Age.45","Age.55")
> Medians<-c(128.25,148.75,158.5,168.75)
> > is.character(Time)
> # [1] TRUE - thus it has no intrinsic numeric value to plot
> > is.numeric(Medians)
> # [1] TRUE
> # coerce Medians to factor and then plot against Time, but can't do
> point/line
> plot(as.factor(Time),Medians,type="p")
> # let R determine the x values (1:4) and omit the x-axis
> plot(Medians,type="b",xaxt="n")
> # add the x-axis with the "Time" labels
> axis(1,at=1:4,labels=Time)
>
>
> On Thu, Jul 13, 2023 at 11:18 AM Sorkin, John 
> wrote:
> >
> > I am trying to plot four points, and join the points with lines. I can
> plot the points, but I can't plot the points and the line.
> > I hope someone can help my with my ggplot code.
> >
> > # load ggplot2
> > if(!require(ggplot2)){install.packages("ggplot2")}
> > library(ggplot2)
> >
> > # Create data
> > Time   <- c("Age.25","Age.35","Age.45","Age.55")
> > Medians<-c(128.25,148.75,158.5,168.75)
> > themedians <- matrix(data=cbind(Time,Medians),nrow=4,ncol=2)
> > dimnames(themedians) <- list(NULL,c("Time","Median"))
> > # Convert to dataframe the data format used by ggplot
> > themedians <- data.frame(themedians)
> > themedians
> >
> > # This plot works
> > ggplot(themedians,aes(x=Time,y=Median))+
> >   geom_point()
> > # This plot does not work!
> > ggplot(themedians,aes(x=Time,y=Median))+
> >   geom_point()+
> >   geom_line()
> >
> > Thank you,
> > John
> >
> > __
> > R-help@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.
>
> __
> R-help@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.
>


-- 
John Kane
Kingston ON Canada

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Variable and value labels

2023-07-13 Thread Anupam Tyagi
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,  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  On Behalf Of Anupam Tyagi
> Sent: Tuesday, July 11, 2023 11:49 PM
> To: r-help mailing list 
> 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@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]]

__
R-help@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.


Re: [R] Variable and value labels

2023-07-13 Thread Adrian Dușa
Anupam,

This is very general, and it also depends on the scientific domain.
Factors do solve the value labels, but they drop the (original) values.
They also lose the connection to the different types of missing values (an
important topic in the social sciences).

Base R provides as much as it can, but it cannot possibly cover all
applications out there, so an add-on package is a must.
A good way to do both variable and value labels, as well as different types
of missing values, is the package "declared".

I hope this helps,
Adrian

On Wed, Jul 12, 2023 at 6:55 AM Anupam Tyagi  wrote:

> 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@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.
>


-- 
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr. 90-92
050663 Bucharest sector 5
Romania
https://adriandusa.eu

[[alternative HTML version deleted]]

__
R-help@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.