0

Strange one this. seq() is creating a mix of numbers and characters, which really screws some things up!

If I create the following sequence:

tmp <- seq(0.1, 2, by=0.1)

Then I get a sequence!

[1] 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 2.0

If I try and get individual elements, then some come up as numbers:

tmp[tmp == 0.2]

0.1

But others don't!

tmp[tmp == 0.3]

numeric(0)

If you search for a character, however:

tmp[tmp == "0.3"]

0.3

The same happens for 0.7, 1.2, 1.3, 1.4, 1.5, 1.7, 1.8 and 1.9. I have no idea what is going on. I am running OSX 10.11.2 ('El Capitan') and R 3.2.3. The same thing happens on two colleagues' computers, running Windows (not sure of the version of R).

Any ideas? Thanks!

  • 3
    [floating precision problem](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Cath Dec 18 '15 at 11:24
  • Thanks! If anyone is interested, `round(seq(0.1, 2, by=0.1), digits=1)` gets round the problem if you want to do something more than just find the number. – Constantly Confused Dec 18 '15 at 11:32
  • 1
    You could round the numbers to, say, 7 digits with `tmp <- round(tmp,7)`. Then the logical subsetting with "==" should work. Generally `all.equal()` is preferable, but in this case `round()` is probably easier and solves the problem. – RHertel Dec 18 '15 at 11:33
  • OK, thanks. Going slightly off-topic, but why is `round()` not a good work around? – Constantly Confused Dec 18 '15 at 11:34
  • Because in order to work correctly you need to specify the digits manually, depending on your problem. The function `all.equal()` is tailored to solve such artifacts resulting from floating-point arithmetics, independent of the value that is considered. – RHertel Dec 18 '15 at 11:37
  • Alright, cheers! Many thanks for the help. Must read instructions more carefully... – Constantly Confused Dec 18 '15 at 11:52

0 Answers0