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(sa

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/UTF

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 <- r

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

2020-07-11 Thread Avi Gross via R-help
ave 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

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.

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 f

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'

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

2020-07-10 Thread William Michels via R-help
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"

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

2020-07-10 Thread Eric Berger
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, > +

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

2020-07-10 Thread Richard O'Keefe
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

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

2020-07-10 Thread Jean-Louis Abitbol
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, F

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

2020-07-10 Thread Bert Gunter
Thanks! As I said, cute exercise. Best, Bert On Fri, Jul 10, 2020 at 1:21 PM Fox, John wrote: > Dear Bert, > > Wouldn't you know it, but your contribution arrived just after I pressed > "send" on my last message? So here's how your solution compares: > > > microbenchmark(John = John <- xn[x]

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

2020-07-10 Thread Fox, John
Dear Bert, Wouldn't you know it, but your contribution arrived just after I pressed "send" on my last message? So here's how your solution compares: > microbenchmark(John = John <- xn[x], +Rich = Rich <- xn[match(x, xc)], +Jeff = Jeff <- { + n

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

2020-07-10 Thread Fox, John
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], +

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

2020-07-10 Thread Bert Gunter
... and continuing with this cute little thread... I found the OP's specification a little imprecise -- are your values always a string that begins with *some sort" of numeric value followed by "some sort" of alpha code? That is, could the numeric value be several digits and the alpha code several

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

2020-07-10 Thread David Carlson
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:

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

2020-07-10 Thread Jeff Newmiller
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 1a1.1 1b1.2 1c1.3 2 2 2a2.1 2b2.2 ", as.is=TRUE, header=TRUE ) tblv <- setNames( tbl$NewCode

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

2020-07-10 Thread Fox, John
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"

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

2020-07-10 Thread Jean-Louis Abitbol
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 accor