Re: [R] How to differ stats_cor labels by group on a ggplot

2020-07-11 Thread Jim Lemon
Okay. If you get stuck, you can always roll your own:

psdf<-read.table("sim_data.txt",header=TRUE,stringsAsFactors=FALSE)
library(plotrix)
source("supsubtext.R")
par(cex.axis=1.5)
for(i in 3:6) {
 png(paste0("TestAge_",names(psdf[i]),".png"),width=600)
 plot(psdf$Age,psdf[,i],xlim=c(20,85),ylim=c(-0.1,0.5),
  xlab="Age",ylab=names(psdf)[i],
  axes=FALSE,pch=19,col=c("green","orange")[psdf$AgeGroup+1],
  cex=2)
 fullaxis(1,col="black",lwd=1.5)
 fullaxis(2,col="black",lwd=1.5)
 # fit the entire column
 lmfit<-lm(psdf[,i]~psdf$Age)
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age))
 cis<-supsmu(psdf$Age,lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 # display the overall confidence band
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="lightgray")
 # now the "young" group
 young<-psdf$AgeGroup == 0
 lmfit<-lm(psdf[young,i]~psdf[young,"Age"])
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age[young]))
 cis<-supsmu(psdf$Age[young],lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="#88ff8880")
 # now the "old" group
 old<-psdf$AgeGroup == 1
 lmfit<-lm(psdf[old,i]~psdf[old,"Age"])
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age[old]))
 cis<-supsmu(psdf$Age[old],lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="#ffaa8880")
 points(psdf$Age,psdf[,i],pch=19,
  col=c("green","orange")[psdf$AgeGroup+1],cex=2)
 # overall correlation
 corval<-cor.test(psdf$Age,psdf[,i])
 corstr<-paste0("R(overall) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,0.05,corstr,sub=2:10,col="blue")
 # young correlation
 corval<-cor.test(psdf[young,"Age"],psdf[young,i])
 corstr<-paste0("R(young) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,0,corstr,sub=2:8,col="green")
 # old correlation
 corval<-cor.test(psdf[old,"Age"],psdf[old,i])
 corstr<-paste0("R(old) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,-0.05,corstr,sub=2:6,col="orange")
 legend(40,0.5,c("Overall","Young","Old"),pch=19,
  col=c("lightgray","green","orange"))
 dev.off()
}

I've been meaning to finish off the "supsubtext" function for a while.

Jim

On Sun, Jul 12, 2020 at 11:20 AM Paulina Skolasinska
 wrote:
>
> Sorry for misspelling your name, Jim. Well, it seems this is not worth the 
> effort then. If my advisor decides this is absolutely essential, I’ll add it 
> in gimp or something. Thanks for giving it a go, Jim.
>
> Get Outlook for iOS
> 
> From: Jim Lemon 
> Sent: Saturday, July 11, 2020 6:32:13 PM
> To: Paulina Skolasinska 
> Cc: r-help@r-project.org 
> Subject: Re: [R] How to differ stats_cor labels by group on a ggplot
>
> Hi Paulina,
> Thanks for the data. Even after downloading a ton of packages along
> with the "ggpubr" package, I get a namespace error when I try to load
> it. After a bit of wrangling, I did get the code to run without the
> "stat_cor" function, producing the four PNG images. My best guess is
> to try the geom_label function with stat="cor", but I can't see any
> argument to format the actual label. I think this is about as far as
> I'm going to get.
>
> Jim
>
> On Sun, Jul 12, 2020 at 12:09 AM Paulina Skolasinska
>  wrote:
> >
> > Thanks Tim, here is the link to the data: https://we.tl/t-c4x9Lw7LeR
> > I'm posting my code again so it better matches the modified data, and 
> > because I've added several improvements in the meantime.
> >
> > for (i in 3:6) {
> > p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> > color=factor(AgeGroup))) +
> > geom_smooth(method="lm") +
> > geom_point(size = 4) +
> > scale_x_continuous(breaks = seq(20,90, by=10)) +
> > scale_y_continuous(breaks = seq(0.1,0.5, by=0.1)) +
> > theme_classic() +
> > expand_limits(y = 0.5) +
> > ylab(names(df)[i]) +
> > theme(axis.text.x = element_text(face="bold", size=14),
> >   axis.text.y = element_text(face="bold", size=14),
> >   axis.title.x = element_text(size=14, face="bold"),
> >   axis.title.y = element_text(size=14, face="bold"),
> >   legend.title = element_text(color="black", size=12),
> >   legend.text = element_text(colour="black", size = 14, face = 
> > "bold")) +
> > geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), 
> > group=1, color="black"), method = "lm") +
> > scale_colour_discrete(name="Group", labels=c("Young", "Old", 
> > "Overall")) +
> > stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i])),
> >  method = "pearson", label.x.npc = c("center"), 
> > label.y.npc="top") +
> > stat_cor(aes(x = Age, y = unlist(df[i

Re: [R] How to differ stats_cor labels by group on a ggplot

2020-07-11 Thread Paulina Skolasinska
Sorry for misspelling your name, Jim. Well, it seems this is not worth the 
effort then. If my advisor decides this is absolutely essential, I�ll add it in 
gimp or something. Thanks for giving it a go, Jim.

Get Outlook for iOS

From: Jim Lemon 
Sent: Saturday, July 11, 2020 6:32:13 PM
To: Paulina Skolasinska 
Cc: r-help@r-project.org 
Subject: Re: [R] How to differ stats_cor labels by group on a ggplot

Hi Paulina,
Thanks for the data. Even after downloading a ton of packages along
with the "ggpubr" package, I get a namespace error when I try to load
it. After a bit of wrangling, I did get the code to run without the
"stat_cor" function, producing the four PNG images. My best guess is
to try the geom_label function with stat="cor", but I can't see any
argument to format the actual label. I think this is about as far as
I'm going to get.

Jim

On Sun, Jul 12, 2020 at 12:09 AM Paulina Skolasinska
 wrote:
>
> Thanks Tim, here is the link to the data: https://we.tl/t-c4x9Lw7LeR
> I'm posting my code again so it better matches the modified data, and because 
> I've added several improvements in the meantime.
>
> for (i in 3:6) {
> p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> color=factor(AgeGroup))) +
> geom_smooth(method="lm") +
> geom_point(size = 4) +
> scale_x_continuous(breaks = seq(20,90, by=10)) +
> scale_y_continuous(breaks = seq(0.1,0.5, by=0.1)) +
> theme_classic() +
> expand_limits(y = 0.5) +
> ylab(names(df)[i]) +
> theme(axis.text.x = element_text(face="bold", size=14),
>   axis.text.y = element_text(face="bold", size=14),
>   axis.title.x = element_text(size=14, face="bold"),
>   axis.title.y = element_text(size=14, face="bold"),
>   legend.title = element_text(color="black", size=12),
>   legend.text = element_text(colour="black", size = 14, face = 
> "bold")) +
> geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), 
> group=1, color="black"), method = "lm") +
> scale_colour_discrete(name="Group", labels=c("Young", "Old", 
> "Overall")) +
> stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i])),
>  method = "pearson", label.x.npc = c("center"), 
> label.y.npc="top") +
> stat_cor(aes(x = Age, y = unlist(df[i]),
>  label = paste(sub("R",expression("R"[overall]),..r.label..), 
> ..p.label.., sep = "~`,`~"),
>  group=1, color="black"), method = "pearson",label.y=0.5, 
> label.x.npc = c("center"),
>  position = position_nudge(y = 0.015 * 0.5))
>
> ggsave(p1, file=paste0("TestAge_", names(df)[i], ".png"), scale=1, 
> width=16, height=10, units="cm")
> }
>
>
> 
> Od: Jim Lemon 
> Wys�ane: sobota, 11 lipca 2020 04:09
> Do: Paulina Skolasinska 
> DW: r-help@r-project.org 
> Temat: Re: [R] How to differ stats_cor labels by group on a ggplot
>
> Hi Paulina,
> Without data it's hard to work out what you are doing. Even a small
> simulated data set would help to get answers.
>
> Jim
>
> On Fri, Jul 10, 2020 at 11:49 PM Paulina Skolasinska
>  wrote:
> >
> > 'm using ggplot2 to plot two variables at a time. I'm plotting two age 
> > groups and overall data on the same graph. I'm also using stat_cor form the 
> > ggpubr package to report correlations for the two groups and overall data.
> >
> > I want each stat_cor label to have a custom subscript - the group name 
> > ("old", "young"). I have managed to do this for the overall data, but I 
> > don't know how to add custom labels for each group separately. I'd like the 
> > labels to look like this: https://imgur.com/a/naF7uNW
> >
> > > for (i in 18:21) {
> >   p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> > color=factor(AgeGroup))) +
> > geom_smooth(method="lm") +
> > geom_point(size = 4) +
> > geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), group=1, 
> > color="black"), method = "lm") +
> > scale_colour_discrete(name="Group", labels=c("young", "old", 
> > "overall")) +
> > stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i]))) +
> > stat_cor(aes(x = Age, y = unlist(df[i]), group=1, color="black",
> >  label = 
> > paste(sub("R",expression("R"[overall]),..r.label..), ..p.label.., sep = 
> > "~`,`~")))
> >
> >   ggsave(p1, file=paste0("Age_", names(df)[i], ".png"), scale=1)
> > }
> > __
> > 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.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see

Re: [R] Character (1a, 1b) to numeric

2020-07-11 Thread Jim Lemon
I'll admit that I cut my teeth on ASCII, but I worried about your
reliance on that ancient typographic ordering. I wrote a little
function:

al2num_sub<-function(x) {
 xspl<-unlist(strsplit(x,""))
 if(length(xspl) > 1)
  xspl<-paste(xspl[1],which(letters==xspl[2]),sep=".")
 return(xspl)
}
unlist(sapply(xc,al2num_sub(xc)))

that does the trick with ASCII, but there was a nagging worry that it
wouldn't work for any ordering apart from the Roman alphabet.
Unfortunately I couldn't find any way to substitute something for
"letters" that would allow me to plug in a more general solution like:

alpha.set<-c("letters","greek",...)

Maybe someone else can crack that one.

Jim

On Sun, Jul 12, 2020 at 9:07 AM Abby Spurdle  wrote:
>
> On Sat, Jul 11, 2020 at 8:04 AM Fox, John  wrote:
> > We've had several solutions, and I was curious about their relative 
> > efficiency. Here's a test
>
> Am I the only person on this mailing list who learnt to program with ASCII...?
>
> In theory, the most ***efficient*** solution, is to get the
> ASCII/UTF8/etc values.
> Then use a simple (math) formula.
> No matching, no searching, required ...
>
> Here's one possibility:
>
> xc <-  c ("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
>
> I <- (nchar (xc) == 2)
> xn <- as.integer (substring (xc, 1, 1) )
> xn [I] <- xn [I] + (utf8ToInt (paste (substring (xc [I], 2, 2),
> collapse="") ) - 96) / 4
> xn
>
> Unfortunately, this makes R look bad.
> The corresponding C implementation is simpler and presumably the
> performance winner.
>
> __
> 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.


Re: [R] How to differ stats_cor labels by group on a ggplot

2020-07-11 Thread Jim Lemon
Hi Paulina,
Thanks for the data. Even after downloading a ton of packages along
with the "ggpubr" package, I get a namespace error when I try to load
it. After a bit of wrangling, I did get the code to run without the
"stat_cor" function, producing the four PNG images. My best guess is
to try the geom_label function with stat="cor", but I can't see any
argument to format the actual label. I think this is about as far as
I'm going to get.

Jim

On Sun, Jul 12, 2020 at 12:09 AM Paulina Skolasinska
 wrote:
>
> Thanks Tim, here is the link to the data: https://we.tl/t-c4x9Lw7LeR
> I'm posting my code again so it better matches the modified data, and because 
> I've added several improvements in the meantime.
>
> for (i in 3:6) {
> p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> color=factor(AgeGroup))) +
> geom_smooth(method="lm") +
> geom_point(size = 4) +
> scale_x_continuous(breaks = seq(20,90, by=10)) +
> scale_y_continuous(breaks = seq(0.1,0.5, by=0.1)) +
> theme_classic() +
> expand_limits(y = 0.5) +
> ylab(names(df)[i]) +
> theme(axis.text.x = element_text(face="bold", size=14),
>   axis.text.y = element_text(face="bold", size=14),
>   axis.title.x = element_text(size=14, face="bold"),
>   axis.title.y = element_text(size=14, face="bold"),
>   legend.title = element_text(color="black", size=12),
>   legend.text = element_text(colour="black", size = 14, face = 
> "bold")) +
> geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), 
> group=1, color="black"), method = "lm") +
> scale_colour_discrete(name="Group", labels=c("Young", "Old", 
> "Overall")) +
> stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i])),
>  method = "pearson", label.x.npc = c("center"), 
> label.y.npc="top") +
> stat_cor(aes(x = Age, y = unlist(df[i]),
>  label = paste(sub("R",expression("R"[overall]),..r.label..), 
> ..p.label.., sep = "~`,`~"),
>  group=1, color="black"), method = "pearson",label.y=0.5, 
> label.x.npc = c("center"),
>  position = position_nudge(y = 0.015 * 0.5))
>
> ggsave(p1, file=paste0("TestAge_", names(df)[i], ".png"), scale=1, 
> width=16, height=10, units="cm")
> }
>
>
> 
> Od: Jim Lemon 
> Wysłane: sobota, 11 lipca 2020 04:09
> Do: Paulina Skolasinska 
> DW: r-help@r-project.org 
> Temat: Re: [R] How to differ stats_cor labels by group on a ggplot
>
> Hi Paulina,
> Without data it's hard to work out what you are doing. Even a small
> simulated data set would help to get answers.
>
> Jim
>
> On Fri, Jul 10, 2020 at 11:49 PM Paulina Skolasinska
>  wrote:
> >
> > 'm using ggplot2 to plot two variables at a time. I'm plotting two age 
> > groups and overall data on the same graph. I'm also using stat_cor form the 
> > ggpubr package to report correlations for the two groups and overall data.
> >
> > I want each stat_cor label to have a custom subscript - the group name 
> > ("old", "young"). I have managed to do this for the overall data, but I 
> > don't know how to add custom labels for each group separately. I'd like the 
> > labels to look like this: https://imgur.com/a/naF7uNW
> >
> > > for (i in 18:21) {
> >   p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> > color=factor(AgeGroup))) +
> > geom_smooth(method="lm") +
> > geom_point(size = 4) +
> > geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), group=1, 
> > color="black"), method = "lm") +
> > scale_colour_discrete(name="Group", labels=c("young", "old", 
> > "overall")) +
> > stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i]))) +
> > stat_cor(aes(x = Age, y = unlist(df[i]), group=1, color="black",
> >  label = 
> > paste(sub("R",expression("R"[overall]),..r.label..), ..p.label.., sep = 
> > "~`,`~")))
> >
> >   ggsave(p1, file=paste0("Age_", names(df)[i], ".png"), scale=1)
> > }
> > __
> > 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.


Re: [R] Character (1a, 1b) to numeric

2020-07-11 Thread Abby Spurdle
On Sat, Jul 11, 2020 at 8:04 AM Fox, John  wrote:
> We've had several solutions, and I was curious about their relative 
> efficiency. Here's a test

Am I the only person on this mailing list who learnt to program with ASCII...?

In theory, the most ***efficient*** solution, is to get the
ASCII/UTF8/etc values.
Then use a simple (math) formula.
No matching, no searching, required ...

Here's one possibility:

xc <-  c ("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")

I <- (nchar (xc) == 2)
xn <- as.integer (substring (xc, 1, 1) )
xn [I] <- xn [I] + (utf8ToInt (paste (substring (xc [I], 2, 2),
collapse="") ) - 96) / 4
xn

Unfortunately, this makes R look bad.
The corresponding C implementation is simpler and presumably the
performance winner.

__
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] Inquiry for R software Licensing.

2020-07-11 Thread Ista Zahn
On Sat, Jul 11, 2020 at 6:20 PM Stephen Ellison  wrote:
>
> > Patrick (Malone Quantitative)
> > There's only one version and it's free.
>
> You're forgetting Microsoft R, formerly Revolution R 
> (https://mran.revolutionanalytics.com/download), which comes in open and 
> enterprise flavours, both with multithreading and the latter with beefed up 
> database access services (from the marketing fluff).

Is there actually an enterprise version? All I've ever seen is MRO,
which hasn't been updated from R 3.5.3.

--Ista

>
> S Ellison
>
> On Sat, Jul 11, 2020 at 1:46 PM Sachin Patil  
> wrote:
> >
> > Dear R Software Team,
> >
> >
> >
> > We want to know about R software Licensing to procure it. Do you have
> > R software License version or any professional version?
> >
> >
> >
> > Regards,
> >
> > Sachin Patil
>
>
>
> ***
> This email and any attachments are confidential. Any u...{{dropped:8}}

__
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] Inquiry for R software Licensing.

2020-07-11 Thread Stephen Ellison
> Patrick (Malone Quantitative)
> There's only one version and it's free.

You're forgetting Microsoft R, formerly Revolution R 
(https://mran.revolutionanalytics.com/download), which comes in open and 
enterprise flavours, both with multithreading and the latter with beefed up 
database access services (from the marketing fluff). I should say I've tried 
neither; the standard R download at R-project.org is fine for what I do.

S Ellison

On Sat, Jul 11, 2020 at 1:46 PM Sachin Patil  
wrote:
>
> Dear R Software Team,
>
>
>
> We want to know about R software Licensing to procure it. Do you have 
> R software License version or any professional version?
>
>
>
> Regards,
>
> Sachin Patil



***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
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] Inquiry for R software Licensing.

2020-07-11 Thread Bert Gunter
... and details for downloading including worldwide repositories (i.e.
*mirrors*) can be found here:
https://cran.r-project.org/

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Jul 11, 2020 at 10:56 AM Patrick (Malone Quantitative) <
mal...@malonequantitative.com> wrote:

> There's only one version and it's free.
>
> On Sat, Jul 11, 2020 at 1:46 PM Sachin Patil
>  wrote:
> >
> > Dear R Software Team,
> >
> >
> >
> > We want to know about R software Licensing to procure it. Do you have R
> > software License version or any professional version?
> >
> >
> >
> > Regards,
> >
> > Sachin Patil
> >
> > IT Executive
> >
> > Vergo Pharma Research Pvt Ltd.
> >
> > (Division - Vergo Clinicals)
> > Plot 24/1 D-1 Mologa de Orora, Corlim,
> >
> > Tiswadi, Goa-403110 India,
> >
> > Tel: +91 8326640555
> >
> >
> >
> >
> > [[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.
>
>
>
> --
> Patrick S. Malone, Ph.D., Malone Quantitative
> NEW Service Models: http://malonequantitative.com
>
> He/Him/His
>
> __
> 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.
>

[[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] Character (1a, 1b) to numeric

2020-07-11 Thread David Winsemius
It might be easier to simply assign names to the numeric vector if you 
already have numeric and character vectors of the right lengths. Using 
Heibergers's vectors:



xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)

names(xn) <- xc

testdata <- rep(c("1", "1a", "1b", "1c",
"2", "2a", "2b", "2c",
   "3", "3a", "3b", "3c"), times=c(1:6,6:1))

xn[ testdata ]  #  NA's when there's no match is a feature.

--
David.

On 7/10/20 7:08 PM, Richard O'Keefe wrote:

This can be done very simply because vectors in R can have
named elements, and can be indexed by strings.


stage <- c("1" = 1, "1a" = 1.3, "1b" = 1.5, "1c" = 1.7,

+"2" = 2, "2a" = 2.3, "2b" = 2.5, "2c" = 2.7,
+"3" = 3, "3a" = 3.3, "3b" = 3.5, "3c" = 3.7)


testdata <- rep(c("1", "1a", "1b", "1c",

+   "2", "2a", "2b", "2c",
+   "3", "3a", "3b", "3c"), times=c(1:6,6:1))


stage[testdata]

   1  1a  1a  1b  1b  1b  1c  1c  1c  1c   2   2   2   2   2  2a  2a  2a  2a
  2a
1.0 1.3 1.3 1.5 1.5 1.5 1.7 1.7 1.7 1.7 2.0 2.0 2.0 2.0 2.0 2.3 2.3 2.3 2.3
2.3
  2a  2b  2b  2b  2b  2b  2b  2c  2c  2c  2c  2c   3   3   3   3  3a  3a  3a
  3b
2.3 2.5 2.5 2.5 2.5 2.5 2.5 2.7 2.7 2.7 2.7 2.7 3.0 3.0 3.0 3.0 3.3 3.3 3.3
3.5
  3b  3c
3.5 3.7

On Sat, 11 Jul 2020 at 05:51, Jean-Louis Abitbol  wrote:


Dear All

I have a character vector,  representing histology stages, such as for
example:
xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")

and this goes on to 3, 3a etc in various order for each patient. I do have
of course a pre-established  classification available which does change
according to the histology criteria under assessment.

I would want to convert xc, for plotting reasons, to a numeric vector such
as

xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)

Unfortunately I have no clue on how to do that.

Thanks for any help and apologies if I am missing the obvious way to do it.

JL
--
Verif30042020

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


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


__
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] Inquiry for R software Licensing.

2020-07-11 Thread Patrick (Malone Quantitative)
There's only one version and it's free.

On Sat, Jul 11, 2020 at 1:46 PM Sachin Patil
 wrote:
>
> Dear R Software Team,
>
>
>
> We want to know about R software Licensing to procure it. Do you have R
> software License version or any professional version?
>
>
>
> Regards,
>
> Sachin Patil
>
> IT Executive
>
> Vergo Pharma Research Pvt Ltd.
>
> (Division - Vergo Clinicals)
> Plot 24/1 D-1 Mologa de Orora, Corlim,
>
> Tiswadi, Goa-403110 India,
>
> Tel: +91 8326640555
>
>
>
>
> [[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.



-- 
Patrick S. Malone, Ph.D., Malone Quantitative
NEW Service Models: http://malonequantitative.com

He/Him/His

__
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] Inquiry for R software Licensing.

2020-07-11 Thread Sachin Patil
Dear R Software Team,

 

We want to know about R software Licensing to procure it. Do you have R
software License version or any professional version?

 

Regards,

Sachin Patil

IT Executive

Vergo Pharma Research Pvt Ltd.

(Division - Vergo Clinicals)
Plot 24/1 D-1 Mologa de Orora, Corlim,

Tiswadi, Goa-403110 India,

Tel: +91 8326640555

 


[[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] Character (1a, 1b) to numeric

2020-07-11 Thread Avi Gross via R-help
There are many ways to do what is requested and some are fairly simple and
robust. A simple switch statement will do if you write some code but
consider using a function from some package for simple vectors or factors.

You could use the recode() or recode_factor() functions in package dplyr or
other similar functions elsewhere and type in the conversions like so:

library("dplyr")

xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")

xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)

sample <- rep(xc, each=3)

recode(sample,
   "1" = 1,
   "1a" = 1.3,
   "1b" = 1.5,
   "1c" = 1.7,
   "2" = 2,
   "2a" =2.3,
   "2b" = 2.5,
   "2c" = 2.7)

That returns:

[1] 1.0 1.0 1.0 1.3 1.3 1.3 1.5 1.5 1.5 1.7 1.7 1.7 2.0 2.0 2.0 2.3 2.3 2.3
2.5 2.5 2.5 2.7 2.7 2.7

To use the original vectors would be a tad harder but doable perhaps using
some indirection.

As has been noted, you need to be careful in matching things to use the
entire item from beginning to end as matching  a substring can produce odd
results. If you add this code to the above, in a silly way, it works for a
more general case:

library(glue)

converted <- sample
for (i in 1:length(xc)) {
  converted <- sub(glue("^{xc[i]}$"), xn[i], converted)
}

result <- as.numeric(converted)

Returns:

> result
 [1] 1.0 1.0 1.0 1.3 1.3 1.3 1.5 1.5 1.5 1.7 1.7 1.7 2.0 2.0 2.0 2.3 2.3 2.3
2.5 2.5 2.5 2.7 2.7 2.7

Not necessarily efficient but it works. You could use something like
glue::glue() to create the arguments you want to use for something like
recode() in more complex cases and so on.

I think we have had enough solutions and methods posted but there are likely
many more as there is rarely only one way to do things in R.

-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Saturday, July 11, 2020 3:02 AM
To: Eric Berger 
Cc: Jean-Louis Abitbol ; R Project Help

Subject: Re: [R] Character (1a, 1b) to numeric

The string index approach works with any mapping from stage names to stage
numbers, not just regular ones.  For example, if we had "1" -> 1, "1a" ->
1.4, "1b" -> 1.6 "2" -> 2, "2a" -> 2.3, "2b" -> 2.7 the 'sub' version would
fail miserably while the string index version would just work.  The 'sub'
version would also not work terribly well if the mapping were "1" -> 1, "a1"
-> 1.3, "b1" -> 1.5, "c1" -> 1.7 and so on. The thing I like about the
indexing approach is that it uses a fundamental operation of the language
very directly.

Anyone using R would do well to *master* what indexing can do for you.


On Sat, 11 Jul 2020 at 17:16, Eric Berger  wrote:

> xn <- as.numeric(sub("c",".7",sub("b",".5",sub("a",".3",xc
>
>
> On Sat, Jul 11, 2020 at 5:09 AM Richard O'Keefe  wrote:
>
>> This can be done very simply because vectors in R can have named 
>> elements, and can be indexed by strings.
>>
>> > stage <- c("1" = 1, "1a" = 1.3, "1b" = 1.5, "1c" = 1.7,
>> +"2" = 2, "2a" = 2.3, "2b" = 2.5, "2c" = 2.7,
>> +"3" = 3, "3a" = 3.3, "3b" = 3.5, "3c" = 3.7)
>>
>> > testdata <- rep(c("1", "1a", "1b", "1c",
>> +   "2", "2a", "2b", "2c",
>> +   "3", "3a", "3b", "3c"), times=c(1:6,6:1))
>>
>> > stage[testdata]
>>   1  1a  1a  1b  1b  1b  1c  1c  1c  1c   2   2   2   2   2  2a  2a  2a
>> 2a
>>  2a
>> 1.0 1.3 1.3 1.5 1.5 1.5 1.7 1.7 1.7 1.7 2.0 2.0 2.0 2.0 2.0 2.3 2.3 
>> 2.3
>> 2.3
>> 2.3
>>  2a  2b  2b  2b  2b  2b  2b  2c  2c  2c  2c  2c   3   3   3   3  3a  3a
>> 3a
>>  3b
>> 2.3 2.5 2.5 2.5 2.5 2.5 2.5 2.7 2.7 2.7 2.7 2.7 3.0 3.0 3.0 3.0 3.3 
>> 3.3
>> 3.3
>> 3.5
>>  3b  3c
>> 3.5 3.7
>>
>> On Sat, 11 Jul 2020 at 05:51, Jean-Louis Abitbol 
>> wrote:
>>
>> > Dear All
>> >
>> > I have a character vector,  representing histology stages, such as 
>> > for
>> > example:
>> > xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
>> >
>> > and this goes on to 3, 3a etc in various order for each patient. I 
>> > do
>> have
>> > of course a pre-established  classification available which does 
>> > change according to the histology criteria under assessment.
>> >
>> > I would want to convert xc, for plotting reasons, to a numeric 
>> > vector
>> such
>> > as
>> >
>> > xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)
>> >
>> > Unfortunately I have no clue on how to do that.
>> >
>> > Thanks for any help and apologies if I am missing the obvious way 
>> > to do
>> it.
>> >
>> > JL
>> > --
>> > Verif30042020
>> >
>> > __
>> > 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.
>> >
>>
>> [[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 re

[R] ODP: How to differ stats_cor labels by group on a ggplot

2020-07-11 Thread Paulina Skolasinska
Thanks Tim, here is the link to the data: https://we.tl/t-c4x9Lw7LeR
I'm posting my code again so it better matches the modified data, and because 
I've added several improvements in the meantime.

for (i in 3:6) {
p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
color=factor(AgeGroup))) +
geom_smooth(method="lm") +
geom_point(size = 4) +
scale_x_continuous(breaks = seq(20,90, by=10)) +
scale_y_continuous(breaks = seq(0.1,0.5, by=0.1)) +
theme_classic() +
expand_limits(y = 0.5) +
ylab(names(df)[i]) +
theme(axis.text.x = element_text(face="bold", size=14),
  axis.text.y = element_text(face="bold", size=14),
  axis.title.x = element_text(size=14, face="bold"),
  axis.title.y = element_text(size=14, face="bold"),
  legend.title = element_text(color="black", size=12),
  legend.text = element_text(colour="black", size = 14, face = 
"bold")) +
geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), group=1, 
color="black"), method = "lm") +
scale_colour_discrete(name="Group", labels=c("Young", "Old", 
"Overall")) +
stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i])),
 method = "pearson", label.x.npc = c("center"), 
label.y.npc="top") +
stat_cor(aes(x = Age, y = unlist(df[i]),
 label = paste(sub("R",expression("R"[overall]),..r.label..), 
..p.label.., sep = "~`,`~"),
 group=1, color="black"), method = "pearson",label.y=0.5, 
label.x.npc = c("center"),
 position = position_nudge(y = 0.015 * 0.5))

ggsave(p1, file=paste0("TestAge_", names(df)[i], ".png"), scale=1, 
width=16, height=10, units="cm")
}



Od: Jim Lemon 
Wys�ane: sobota, 11 lipca 2020 04:09
Do: Paulina Skolasinska 
DW: r-help@r-project.org 
Temat: Re: [R] How to differ stats_cor labels by group on a ggplot

Hi Paulina,
Without data it's hard to work out what you are doing. Even a small
simulated data set would help to get answers.

Jim

On Fri, Jul 10, 2020 at 11:49 PM Paulina Skolasinska
 wrote:
>
> 'm using ggplot2 to plot two variables at a time. I'm plotting two age groups 
> and overall data on the same graph. I'm also using stat_cor form the ggpubr 
> package to report correlations for the two groups and overall data.
>
> I want each stat_cor label to have a custom subscript - the group name 
> ("old", "young"). I have managed to do this for the overall data, but I don't 
> know how to add custom labels for each group separately. I'd like the labels 
> to look like this: https://imgur.com/a/naF7uNW
>
> > for (i in 18:21) {
>   p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
> color=factor(AgeGroup))) +
> geom_smooth(method="lm") +
> geom_point(size = 4) +
> geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), group=1, 
> color="black"), method = "lm") +
> scale_colour_discrete(name="Group", labels=c("young", "old", "overall")) +
> stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i]))) +
> stat_cor(aes(x = Age, y = unlist(df[i]), group=1, color="black",
>  label = paste(sub("R",expression("R"[overall]),..r.label..), 
> ..p.label.., sep = "~`,`~")))
>
>   ggsave(p1, file=paste0("Age_", names(df)[i], ".png"), scale=1)
> }
> __
> 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.

[[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] Character (1a, 1b) to numeric

2020-07-11 Thread William Michels via R-help
Agreed, I meant to add this line (for unclassed factor levels 1-through-8):

> ((1:8 - 1)*(0.25))+1
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75

Depending on the circumstance, you can also consider using dummy
factors or even "NA" as a level; see the "factor" help page for
details.

Best, Bill.

W. Michels, Ph.D.



On Sat, Jul 11, 2020 at 12:16 AM Jean-Louis Abitbol  wrote:
>
> Hello Bill,
>
> Thanks.
>
> That has indeed the advantage of keeping the histology classification on the  
> plot instead of some arbitrary numeric scale.
>
> Best wishes, JL
>
> On Sat, Jul 11, 2020, at 8:25 AM, William Michels wrote:
> > Hello Jean-Louis,
> >
> > Noting the subject line of your post I thought the first answer would
> > have been encoding histology stages as factors, and "unclass-ing" them
> > to obtain integers that then can be mathematically manipulated. You
> > can get a lot of work done with all the commands listed on the
> > "factor" help page:
> >
> > ?factor
> > samples <- 1:36
> > values <- runif(length(samples), min=1, max=length(samples))
> > hist <- rep(c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c"), times=1:8)
> > data1 <- data.frame("samples" = samples, "values" = values, "hist" = hist )
> > (data1$hist <- factor(data1$hist, levels=c("1", "1a", "1b", "1c", "2",
> > "2a", "2b", "2c")) )
> > unclass(data1$hist)
> >
> > library(RColorBrewer); pal_1 <- brewer.pal(8, "Pastel2")
> > barplot(data1$value, beside=T, col=pal_1[data1$hist])
> > plot(data1$hist, data1$value, col=pal_1)
> > pal_2 <- brewer.pal(8, "Dark2")
> > plot(unclass(data1$hist)/4, data1$value, pch=19, col=pal_2[data1$hist] )
> > group <- c(rep(0,10),rep(1,26)); data1$group <- group
> > library(lattice); dotplot(hist ~ values | group, data=data1, xlim=c(0,36) )
> >
> > HTH, Bill.
> >
> > W. Michels, Ph.D.
> >
> >
> >
> >
> > On Fri, Jul 10, 2020 at 1:41 PM Jean-Louis Abitbol  wrote:
> > >
> > > Many thanks to all. This help-list is wonderful.
> > >
> > > I have used Rich Heiberger solution using match and found something to 
> > > learn in each answer.
> > >
> > > off topic, I also enjoyed very much his 2008 paper on the graphical 
> > > presentation of safety data
> > >
> > > Best wishes.
> > >
> > >
> > > On Fri, Jul 10, 2020, at 10:02 PM, Fox, John wrote:
> > > > Hi,
> > > >
> > > > We've had several solutions, and I was curious about their relative
> > > > efficiency. Here's a test with a moderately large data vector:
> > > >
> > > > > library("microbenchmark")
> > > > > set.seed(123) # for reproducibility
> > > > > x <- sample(xc, 1e4, replace=TRUE) # "data"
> > > > > microbenchmark(John = John <- xn[x],
> > > > +Rich = Rich <- xn[match(x, xc)],
> > > > +Jeff = Jeff <- {
> > > > + n <- as.integer( sub( "[a-i]$", "", x ) )
> > > > + d <- match( sub( "^\\d+", "", x ), letters[1:9] )
> > > > + d[ is.na( d ) ] <- 0
> > > > + n + d / 10
> > > > + },
> > > > +David = David <- as.numeric(gsub("a", ".3",
> > > > +  gsub("b", ".5",
> > > > +   gsub("c", ".7", x,
> > > > +times=1000L
> > > > +)
> > > > Unit: microseconds
> > > >   expr   minlq   mean median uq   max 
> > > > neval cld
> > > >   John   228.816   345.371   513.5614   503.5965   533.0635  10829.08  
> > > > 1000 a
> > > >   Rich   217.395   343.035   534.2074   489.0075   518.3260  15388.96  
> > > > 1000 a
> > > >   Jeff 10325.471 13070.737 15387.2545 15397.9790 17204.0115 153486.94  
> > > > 1000  b
> > > >  David 14256.673 18148.492 20185.7156 20170.3635 22067.6690  34998.95  
> > > > 1000   c
> > > > > all.equal(John, Rich)
> > > > [1] TRUE
> > > > > all.equal(John, David)
> > > > [1] "names for target but not for current"
> > > > > all.equal(John, Jeff)
> > > > [1] "names for target but not for current" "Mean relative difference:
> > > > 0.1498243"
> > > >
> > > > Of course, efficiency isn't the only consideration, and aesthetically
> > > > (and no doubt subjectively) I prefer Rich Heiberger's solution. OTOH,
> > > > Jeff's solution is more general in that it generates the correspondence
> > > > between letters and numbers. The argument for Jeff's solution would,
> > > > however, be stronger if it gave the desired answer.
> > > >
> > > > Best,
> > > >  John
> > > >
> > > > > On Jul 10, 2020, at 3:28 PM, David Carlson  wrote:
> > > > >
> > > > > Here is a different approach:
> > > > >
> > > > > xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
> > > > > xn <- as.numeric(gsub("a", ".3", gsub("b", ".5", gsub("c", ".7", 
> > > > > xc
> > > > > xn
> > > > > # [1] 1.0 1.3 1.5 1.7 2.0 2.3 2.5 2.7
> > > > >
> > > > > David L Carlson
> > > > > Professor Emeritus of Anthropology
> > > > > Texas A&M University
> > > > >
> > > > > On Fri, Jul 10, 2020 at 1:10 PM Fox, John  wrote:
> > > > > Dear Jean-Louis,
> >

Re: [R] Character (1a, 1b) to numeric

2020-07-11 Thread Jean-Louis Abitbol
Hello Bill,

Thanks.

That has indeed the advantage of keeping the histology classification on the  
plot instead of some arbitrary numeric scale.

Best wishes, JL

On Sat, Jul 11, 2020, at 8:25 AM, William Michels wrote:
> Hello Jean-Louis,
> 
> Noting the subject line of your post I thought the first answer would
> have been encoding histology stages as factors, and "unclass-ing" them
> to obtain integers that then can be mathematically manipulated. You
> can get a lot of work done with all the commands listed on the
> "factor" help page:
> 
> ?factor
> samples <- 1:36
> values <- runif(length(samples), min=1, max=length(samples))
> hist <- rep(c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c"), times=1:8)
> data1 <- data.frame("samples" = samples, "values" = values, "hist" = hist )
> (data1$hist <- factor(data1$hist, levels=c("1", "1a", "1b", "1c", "2",
> "2a", "2b", "2c")) )
> unclass(data1$hist)
> 
> library(RColorBrewer); pal_1 <- brewer.pal(8, "Pastel2")
> barplot(data1$value, beside=T, col=pal_1[data1$hist])
> plot(data1$hist, data1$value, col=pal_1)
> pal_2 <- brewer.pal(8, "Dark2")
> plot(unclass(data1$hist)/4, data1$value, pch=19, col=pal_2[data1$hist] )
> group <- c(rep(0,10),rep(1,26)); data1$group <- group
> library(lattice); dotplot(hist ~ values | group, data=data1, xlim=c(0,36) )
> 
> HTH, Bill.
> 
> W. Michels, Ph.D.
> 
> 
> 
> 
> On Fri, Jul 10, 2020 at 1:41 PM Jean-Louis Abitbol  wrote:
> >
> > Many thanks to all. This help-list is wonderful.
> >
> > I have used Rich Heiberger solution using match and found something to 
> > learn in each answer.
> >
> > off topic, I also enjoyed very much his 2008 paper on the graphical 
> > presentation of safety data
> >
> > Best wishes.
> >
> >
> > On Fri, Jul 10, 2020, at 10:02 PM, Fox, John wrote:
> > > Hi,
> > >
> > > We've had several solutions, and I was curious about their relative
> > > efficiency. Here's a test with a moderately large data vector:
> > >
> > > > library("microbenchmark")
> > > > set.seed(123) # for reproducibility
> > > > x <- sample(xc, 1e4, replace=TRUE) # "data"
> > > > microbenchmark(John = John <- xn[x],
> > > +Rich = Rich <- xn[match(x, xc)],
> > > +Jeff = Jeff <- {
> > > + n <- as.integer( sub( "[a-i]$", "", x ) )
> > > + d <- match( sub( "^\\d+", "", x ), letters[1:9] )
> > > + d[ is.na( d ) ] <- 0
> > > + n + d / 10
> > > + },
> > > +David = David <- as.numeric(gsub("a", ".3",
> > > +  gsub("b", ".5",
> > > +   gsub("c", ".7", x,
> > > +times=1000L
> > > +)
> > > Unit: microseconds
> > >   expr   minlq   mean median uq   max 
> > > neval cld
> > >   John   228.816   345.371   513.5614   503.5965   533.0635  10829.08  
> > > 1000 a
> > >   Rich   217.395   343.035   534.2074   489.0075   518.3260  15388.96  
> > > 1000 a
> > >   Jeff 10325.471 13070.737 15387.2545 15397.9790 17204.0115 153486.94  
> > > 1000  b
> > >  David 14256.673 18148.492 20185.7156 20170.3635 22067.6690  34998.95  
> > > 1000   c
> > > > all.equal(John, Rich)
> > > [1] TRUE
> > > > all.equal(John, David)
> > > [1] "names for target but not for current"
> > > > all.equal(John, Jeff)
> > > [1] "names for target but not for current" "Mean relative difference:
> > > 0.1498243"
> > >
> > > Of course, efficiency isn't the only consideration, and aesthetically
> > > (and no doubt subjectively) I prefer Rich Heiberger's solution. OTOH,
> > > Jeff's solution is more general in that it generates the correspondence
> > > between letters and numbers. The argument for Jeff's solution would,
> > > however, be stronger if it gave the desired answer.
> > >
> > > Best,
> > >  John
> > >
> > > > On Jul 10, 2020, at 3:28 PM, David Carlson  wrote:
> > > >
> > > > Here is a different approach:
> > > >
> > > > xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
> > > > xn <- as.numeric(gsub("a", ".3", gsub("b", ".5", gsub("c", ".7", xc
> > > > xn
> > > > # [1] 1.0 1.3 1.5 1.7 2.0 2.3 2.5 2.7
> > > >
> > > > David L Carlson
> > > > Professor Emeritus of Anthropology
> > > > Texas A&M University
> > > >
> > > > On Fri, Jul 10, 2020 at 1:10 PM Fox, John  wrote:
> > > > Dear Jean-Louis,
> > > >
> > > > There must be many ways to do this. Here's one simple way (with no 
> > > > claim of optimality!):
> > > >
> > > > > xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
> > > > > xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)
> > > > >
> > > > > set.seed(123) # for reproducibility
> > > > > x <- sample(xc, 20, replace=TRUE) # "data"
> > > > >
> > > > > names(xn) <- xc
> > > > > z <- xn[x]
> > > > >
> > > > > data.frame(z, x)
> > > >  z  x
> > > > 1  2.5 2b
> > > > 2  2.5 2b
> > > > 3  1.5 1b
> > > > 4  2.3 2a
> > > > 5  1.5 1b
> > > > 6  1.3 1a
> > > > 7  1.3 1a
> > > > 8  2.3 2a
> > > > 9  1.5

Re: [R] Character (1a, 1b) to numeric

2020-07-11 Thread Richard O'Keefe
The string index approach works with any mapping from stage names
to stage numbers, not just regular ones.  For example, if we had
"1" -> 1, "1a" -> 1.4, "1b" -> 1.6
"2" -> 2, "2a" -> 2.3, "2b" -> 2.7
the 'sub' version would fail miserably while the string index
version would just work.  The 'sub' version would also not work
terribly well if the mapping were
"1" -> 1, "a1" -> 1.3, "b1" -> 1.5, "c1" -> 1.7
and so on. The thing I like about the indexing approach is that
it uses a fundamental operation of the language very directly.

Anyone using R would do well to *master* what indexing can do
for you.


On Sat, 11 Jul 2020 at 17:16, Eric Berger  wrote:

> xn <- as.numeric(sub("c",".7",sub("b",".5",sub("a",".3",xc
>
>
> On Sat, Jul 11, 2020 at 5:09 AM Richard O'Keefe  wrote:
>
>> This can be done very simply because vectors in R can have
>> named elements, and can be indexed by strings.
>>
>> > stage <- c("1" = 1, "1a" = 1.3, "1b" = 1.5, "1c" = 1.7,
>> +"2" = 2, "2a" = 2.3, "2b" = 2.5, "2c" = 2.7,
>> +"3" = 3, "3a" = 3.3, "3b" = 3.5, "3c" = 3.7)
>>
>> > testdata <- rep(c("1", "1a", "1b", "1c",
>> +   "2", "2a", "2b", "2c",
>> +   "3", "3a", "3b", "3c"), times=c(1:6,6:1))
>>
>> > stage[testdata]
>>   1  1a  1a  1b  1b  1b  1c  1c  1c  1c   2   2   2   2   2  2a  2a  2a
>> 2a
>>  2a
>> 1.0 1.3 1.3 1.5 1.5 1.5 1.7 1.7 1.7 1.7 2.0 2.0 2.0 2.0 2.0 2.3 2.3 2.3
>> 2.3
>> 2.3
>>  2a  2b  2b  2b  2b  2b  2b  2c  2c  2c  2c  2c   3   3   3   3  3a  3a
>> 3a
>>  3b
>> 2.3 2.5 2.5 2.5 2.5 2.5 2.5 2.7 2.7 2.7 2.7 2.7 3.0 3.0 3.0 3.0 3.3 3.3
>> 3.3
>> 3.5
>>  3b  3c
>> 3.5 3.7
>>
>> On Sat, 11 Jul 2020 at 05:51, Jean-Louis Abitbol 
>> wrote:
>>
>> > Dear All
>> >
>> > I have a character vector,  representing histology stages, such as for
>> > example:
>> > xc <-  c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
>> >
>> > and this goes on to 3, 3a etc in various order for each patient. I do
>> have
>> > of course a pre-established  classification available which does change
>> > according to the histology criteria under assessment.
>> >
>> > I would want to convert xc, for plotting reasons, to a numeric vector
>> such
>> > as
>> >
>> > xn <- c(1, 1.3, 1.5, 1.7, 2, 2.3, 2.5, 2.7)
>> >
>> > Unfortunately I have no clue on how to do that.
>> >
>> > Thanks for any help and apologies if I am missing the obvious way to do
>> it.
>> >
>> > JL
>> > --
>> > Verif30042020
>> >
>> > __
>> > 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.
>> >
>>
>> [[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.
>>
>

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