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
> 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) > testdata <- rep(c("1", "1a", "1b", "1c", "2", "2a", "2b", "2c"), times=1:8) > testdata[1] "1" "1a" "1a" "1b" "1b" "1b" "1c" "1c" "1c" "1c" "2" "2" "2" "2" "2" [16] "2a" "2a" "2a" "2a" "2a" "2a" "2b" "2b" "2b" "2b" "2b" "2b" "2b" "2c" "2c" [31] "2c" "2c" "2c" "2c" "2c" "2c"> ?match > xn[match(testdata, xc)][1] 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 [20] 2.3 2.3 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7>On Fri, Jul 10, 2020 at 1:51 PM Jean-Louis Abitbol <abitbol at 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 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.
Obvious is in the eye of the beholder. Presuming your letters don't go beyond "i": a) Lookup table: tbl <- read.table( text"OldCode NewCode 1 1 1a 1.1 1b 1.2 1c 1.3 2 2 2a 2.1 2b 2.2 ", as.is=TRUE, header=TRUE ) tblv <- setNames( tbl$NewCode, tbl$OldCode ) test <- c( "2", "1c", "2b" ) as.vector( tblv[ test ] ) b) String manipulation: n <- as.integer( sub( "[a-i]$", "", test ) ) d <- match( sub( "^\\d+", "", test ), letters[1:9] ) d[ is.na( d ) ] <- 0 n + d / 10 On July 10, 2020 10:50:18 AM PDT, Jean-Louis Abitbol <abitbol at 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-- Sent from my phone. Please excuse my brevity.
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 <abitbol at 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 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. >[[alternative HTML version deleted]]
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 <raoknz at gmail.com> 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 <abitbol at 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 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
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 <abitbol at 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 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. >> > [[alternative HTML version deleted]] > > ______________________________________________ > 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.