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

David Carlson dc@r|@on @end|ng |rom t@mu@edu
Fri Jul 10 21:28:03 CEST 2020


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 <jfox using mcmaster.ca> 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 1b
> 10 2.0  2
> 11 1.7 1c
> 12 2.3 2a
> 13 2.3 2a
> 14 1.0  1
> 15 1.3 1a
> 16 1.5 1b
> 17 2.7 2c
> 18 2.0  2
> 19 1.5 1b
> 20 1.5 1b
>
> I hope this helps,
>  John
>
>   -----------------------------
>   John Fox, Professor Emeritus
>   McMaster University
>   Hamilton, Ontario, Canada
>   Web: http::/socserv.mcmaster.ca/jfox
>
> > On Jul 10, 2020, at 1:50 PM, Jean-Louis Abitbol <abitbol using sent.com>
> 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!V7p9rtNSgBWmF3KJ3U_01fR7vP_I7y-OnWHiTFxwRZ6bVJ3-emOwkBtcU3rSW6I$
> > PLEASE do read the posting guide
> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!V7p9rtNSgBWmF3KJ3U_01fR7vP_I7y-OnWHiTFxwRZ6bVJ3-emOwkBtcg7nzsmk$
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!V7p9rtNSgBWmF3KJ3U_01fR7vP_I7y-OnWHiTFxwRZ6bVJ3-emOwkBtcU3rSW6I$
> PLEASE do read the posting guide
> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!V7p9rtNSgBWmF3KJ3U_01fR7vP_I7y-OnWHiTFxwRZ6bVJ3-emOwkBtcg7nzsmk$
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list