[R] decimal number format as quarter

David Reiner David.Reiner at xrtrading.com
Wed Feb 8 15:48:12 CET 2012


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

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Arnaud Gaboury
Sent: Wednesday, February 08, 2012 8:45 AM
To: jim holtman
Cc: r-help at r-project.org
Subject: Re: [R] decimal number format as quarter

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.

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


This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates.

THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.



More information about the R-help mailing list