[R] decimal number format as quarter
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Feb 8 16:19:57 CET 2012
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
More information about the R-help
mailing list