Hello, I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. All numbers are in fact 2.1/4, 2.1/2, 2.3/4. How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ? TY for any help. Arnaud Gaboury ? A2CT2 Ltd.
will this do it for you:> x <- c(2.2, 2.4, 2.6, 3.2, 3.4, 3.6) > # get integer part > x.i <- as.integer(x) > # get fractional part > x.f <- (x * 10) %% 10 > # new result > result <- x.i + ifelse(x.f == 2+ , .25 + , ifelse(x.f == 4 + , .5 + , .75 + ) + )> result[1] 2.25 2.50 2.75 3.25 3.50 3.75> >On Wed, Feb 8, 2012 at 9:12 AM, Arnaud Gaboury <arnaud.gaboury at a2ct2.com> wrote:> Hello, > > I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. > > On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. > All numbers are in fact 2.1/4, 2.1/2, 2.3/4. > > How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ? > > TY for any help. > > Arnaud Gaboury > > A2CT2 Ltd. > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
TY Jim, It do the trick. I was trying to play without success with the format() options. No simplest way so? Arnaud Gaboury ? A2CT2 Ltd. -----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: mercredi 8 f?vrier 2012 15:36 To: Arnaud Gaboury Cc: r-help at r-project.org Subject: Re: [R] decimal number format as quarter will this do it for you:> x <- c(2.2, 2.4, 2.6, 3.2, 3.4, 3.6) > # get integer part > x.i <- as.integer(x) > # get fractional part > x.f <- (x * 10) %% 10 > # new result > result <- x.i + ifelse(x.f == 2+ , .25 + , ifelse(x.f == 4 + , .5 + , .75 + ) + )> result[1] 2.25 2.50 2.75 3.25 3.50 3.75> >On Wed, Feb 8, 2012 at 9:12 AM, Arnaud Gaboury <arnaud.gaboury at a2ct2.com> wrote:> Hello, > > I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. > > On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. > All numbers are in fact 2.1/4, 2.1/2, 2.3/4. > > How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ? > > TY for any help. > > Arnaud Gaboury > > A2CT2 Ltd. > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Feb 8, 2012, at 9:12 AM, Arnaud Gaboury wrote:> Hello, > > I have to deal with numbers with a decimal part as quarter, coming > from two systems with different way to show decimals. I need to tell > R these are in fact the same number. > > On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On > the other side, I have 2.25, 2.50 and 2.75. > All numbers are in fact 2.1/4, 2.1/2, 2.3/4. > > How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ?vec <- c( 2.2 , 2.4 , 2.6) trunc(vec) + (vec-trunc(vec))*(2.5/2) [1] 2.25 2.50 2.75> > TY for any help. > > Arnaud Gaboury > > A2CT2 Ltd. > > ______________________________________________ > R-help at r-project.org mailing list > 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.David Winsemius, MD West Hartford, CT
Here is a shorter way:> x <- c(2.2, 2.4, 2.6, 3.2, 3.4, 3.6) > # get integer part > x.i <- as.integer(x) > # get fractional part > x.f <- x %% 1 > result <- x.i + x.f * 1.25 > result[1] 2.25 2.50 2.75 3.25 3.50 3.75> as.integer(x) + (x %% 1) * 1.25[1] 2.25 2.50 2.75 3.25 3.50 3.75>On Wed, Feb 8, 2012 at 9:44 AM, Arnaud Gaboury <arnaud.gaboury at a2ct2.com> wrote:> TY Jim, > > It do the trick. > > I was trying to play without success with the format() options. > No simplest way so? > > Arnaud Gaboury > > A2CT2 Ltd. > > > -----Original Message----- > From: jim holtman [mailto:jholtman at gmail.com] > Sent: mercredi 8 f?vrier 2012 15:36 > To: Arnaud Gaboury > Cc: r-help at r-project.org > Subject: Re: [R] decimal number format as quarter > > will this do it for you: > >> x <- c(2.2, 2.4, 2.6, 3.2, 3.4, 3.6) >> # get integer part >> x.i <- as.integer(x) >> # get fractional part >> x.f <- (x * 10) %% 10 >> # new result >> result <- x.i + ifelse(x.f == 2 > + ? ? ? ? ? ? ? ? ? ? , .25 > + ? ? ? ? ? ? ? ? ? ? , ifelse(x.f == 4 > + ? ? ? ? ? ? ? ? ? ? ? ? , .5 > + ? ? ? ? ? ? ? ? ? ? ? ? , .75 > + ? ? ? ? ? ? ? ? ? ? ? ? ) > + ? ? ? ? ? ? ? ? ? ? ) >> result > [1] 2.25 2.50 2.75 3.25 3.50 3.75 >> >> > > On Wed, Feb 8, 2012 at 9:12 AM, Arnaud Gaboury <arnaud.gaboury at a2ct2.com> wrote: >> Hello, >> >> I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. >> >> On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. >> All numbers are in fact 2.1/4, 2.1/2, 2.3/4. >> >> How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ? >> >> TY for any help. >> >> Arnaud Gaboury >> >> A2CT2 Ltd. >> >> ______________________________________________ >> R-help at r-project.org mailing list >> 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. > > > > -- > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Wed, Feb 8, 2012 at 9:12 AM, Arnaud Gaboury <arnaud.gaboury at a2ct2.com> wrote:> Hello, > > I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. > > On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. > All numbers are in fact 2.1/4, 2.1/2, 2.3/4. > > How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ? >In this solution we break apart the portion before and after the dot and divide the portion after the dot by 8 (assuming its supposed to represent the number of eighths):> library(gsubfn) > x <- c(2.2, 2.4, 2.6) > strapply(x, "(\\d+).(\\d+)", ~ as.numeric(x) + as.numeric(y) / 8, simplify = TRUE)[1] 2.25 2.50 2.75 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Wed, Feb 08, 2012 at 03:12:56PM +0100, Arnaud Gaboury wrote:> Hello, > > I have to deal with numbers with a decimal part as quarter, coming from two systems with different way to show decimals. I need to tell R these are in fact the same number. > > On one side my number are formatted this way : 2.2 , 2.4 and 2.6. On the other side, I have 2.25, 2.50 and 2.75. > All numbers are in fact 2.1/4, 2.1/2, 2.3/4. > > How can I tell R 2.2 is 2.25, 2.4 is 2.50 and 2.6 is 2.75 ?Hello: Try the following. vec <- c(2, 2.2, 2.4, 2.6, 3, 3.2, 3.4, 3.6) round(4*(vec + 0.1))/4 [1] 2.00 2.25 2.50 2.75 3.00 3.25 3.50 3.75 Hope this helps. Petr Savicky.
Thanks so much Peter. You are the man. Easy way and working for me. Arnaud Gaboury ? A2CT2 Ltd. -----Original Message----- From: peter dalgaard [mailto:pdalgd at gmail.com] Sent: mercredi 8 f?vrier 2012 16:15 To: David Reiner Cc: Arnaud Gaboury; jim holtman; r-help at r-project.org Subject: Re: [R] decimal number format as quarter On Feb 8, 2012, at 15:48 , David Reiner wrote:> Looks like something priced in eighths; we deal with similar notation for bonds and similar instruments. > >> x <- c(2.2, 2.4, 2.6, 3.2, 3.4, 3.6) >> as.integer(x)+10*(x-as.integer(x))/8 > [1] 2.25 2.50 2.75 3.25 3.50 3.75 > > Adjust the 10 and 8 if you have other denominators.For the case at hand, I expect that you can even get away with> ceiling(x*4)/4[1] 2.25 2.50 2.75 3.25 3.50 3.75 -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com