Strange behavior of the match function in R

I am trying to figure out why the function match

shows strange behavior when comparing two numeric vectors. Obviously it has to do with the precision of the values, but I couldn't find a good description of the problem. I was able to solve the problem with zapsmall

, but wondering if there is a better way.

Example:

a <- seq(1,1.9,0.1)
a
# [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

b <- seq(0,1.9,0.1)
b
# [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

match(a,b)
# [1] 11 12 NA 14 NA 16 17 18 19 20

match(zapsmall(a),zapsmall(b))
# [1] 11 12 13 14 15 16 17 18 19 20

      

+3


source to share


2 answers


I found that one possible solution is to use pmatch

("Partial String Matching"), although the functions are first converted to a character vector with as.character

. I'm sure there must be situations where this can cause problems, but this works for this case:



pmatch(a,b)
#[1] 11 12 13 14 15 16 17 18 19 20

      

0


source


I found out that

a <- 10:19 / 10
a
# [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

b <- 0:19 / 10
b
# [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

match(a,b)
#  [1] 11 12 13 14 15 16 17 18 19 20

      



works well (no character conversion), so the problem might be "seq" and not "match".

0


source







All Articles