Q:

Take "a" and "b" as being equal if they are the same in their first 4 decimal places in R

I was wondering how I could have R take any two objects such as "a" and "b" (shown below) as being equal if they are EXACTLY the same in their first 4 decimal places?
P.S. Thus, I'm asking how I could make a conditional command out of this question.
a = 1.234574789

b = 1.234565638

A:

We can check the first 6 places, with substr
substr(a, 1, 6)== substr(b, 1, 6)

Or with sprintf
sprintf("%0.4f", a) == sprintf("%0.4f", b)
#[1] TRUE

Can create a function with this
f1 <- function(v1, v2) {
        sprintf("%0.4f", v1) == sprintf("%0.4f", v2)
 }

f1(a, b)
#[1] TRUE

f1(1.2345, 1.2346)
#[1] FALSE

and if we need to round, then use round 
round(a, 4)
#[1] 1.2346
round(b, 4)
#[1] 1.2346

Also, the last solution can be also compared

A:

If you want to see how to code a custom function:
> equal <- function(a, b, sig=4) { return (round(a,sig) == round(b,sig)) }
> equal(1.23456, 1.23457)
[1] TRUE
> equal(1.23456, 1.23557)
[1] FALSE

