1

I have a data frame called 'x', several columns and 592 rows.

    > x[x$n.bounces >= 1, ][["magnitude"]]
  [1] 0.30 0.50 0.30 0.20 0.30 0.20 0.70 0.50 0.40 0.50 1.00 1.30 0.50 0.20 2.00 0.40 0.50 3.50 0.20 0.50 0.30 0.21 0.50 0.20 0.80
 [26] 0.30 0.30 0.60 0.30 1.00 1.90 1.20 0.30 1.50 0.81 0.20 0.80 0.60 3.40 1.20 2.40 1.40 0.20 1.70 1.20 1.40 2.40 0.40 0.80 1.70
 [51] 0.80 0.80 0.48 0.70 0.20 0.80 8.30 0.50 3.80 1.30 0.50 2.90 0.40 1.80 3.20 2.10 0.40 0.90 1.30 0.40 1.80 0.20 1.30 0.30 0.50
 [76] 0.30 0.70 0.30 0.40 2.00 0.60 0.30 0.70 1.10 0.20 2.70 0.40 0.80 1.90 0.30 0.60 1.00 1.30 1.10 0.90 0.50 0.70 0.70 0.20 0.30
[101] 0.20 0.50 2.10 1.60 0.20 0.20 0.60 0.20 0.33 0.60 0.60 0.30 0.50 0.40 0.50 0.30 0.70 0.90 0.50 0.60 1.50 3.20 0.20 4.20 2.30
[126] 1.40 0.30 0.50 0.20 0.90 0.50 3.10 0.50 0.20 2.30 0.60 0.20 5.50 0.50 1.00 1.00 0.60 1.50 3.00 0.80 1.30 0.20 4.09 0.20 0.40
[151] 0.20 0.20 1.30 0.50 1.00 2.10 1.50 1.70 0.20 3.60 1.60 0.20 0.20 0.70 1.50 0.20 0.20 1.00 0.30 0.30 2.80 0.80 0.30 0.20 0.23
[176] 2.00 1.00 0.92 0.80 0.33 0.80 1.10 0.20 0.60 1.50 0.30 0.53 0.35 0.30 0.43 0.50 8.80 0.20 0.30 1.10 0.22 4.70 0.20 0.21 1.10
[201] 0.30 1.50 0.50 0.20 0.20 0.20 0.64 0.20 0.80 0.20 0.43 0.40 0.20 0.40 0.40 0.20 0.30

> dput(x[x$n.bounces >= 1, ][["magnitude"]])
c(0.3, 0.5, 0.3, 0.2, 0.3, 0.2, 0.7,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, 0.2, 0.4, 
0.4, 0.2, 0.3)

now i want to select only the ones with "magnitude" < 0.3:

> x[x$n.bounces >= 1 & x$magnitude < 0.3, ][["magnitude"]]
 [1] 0.20 0.30 0.20 0.20 0.20 0.21 0.20 0.30 0.20 0.20 0.20 0.20 0.30 0.30 0.30 0.20 0.20 0.20 0.20 0.20 0.20 0.30 0.20 0.20 0.20
[26] 0.20 0.20 0.20 0.20 0.20 0.20 0.20 0.20 0.20 0.20 0.30 0.20 0.23 0.20 0.30 0.20 0.30 0.22 0.20 0.21 0.20 0.20 0.20 0.20 0.20
[51] 0.20 0.20

how come there are still several (but not all!) values with 0.30 in this selection? i tried other values and it seems it only doesn't work for values < 1.0

originally the data comes from a csv file.

if you need more information, please let me know. I am very new to R and didn't know what might be of help...

  • Looks like a floating point representation error – James Jan 24 '13 at 12:31
  • What is the class of x$magnitude ? – juba Jan 24 '13 at 12:40
  • 1
    By the way, you could simplify `x[x$n.bounces >= 1, ][["magnitude"]]` into `x[x$n.bounces >= 1, "magnitude"]` – juba Jan 24 '13 at 12:41
  • try something like `print(y["magnitude"], digits = 22)` where y is you final subset... – agstudy Jan 24 '13 at 12:42
  • `> class(x$magnitude) [1] "numeric" > typeof(x$magnitude) [1] "double" > print(x[x$n.bounces >= 1 & x$magnitude < 0.3, "magnitude"], digits = 22) [1] [1] 0.1999999999999999555911 0.2999999999999998223643 0.1999999999999999555911` (first 3 value of same subset as above) – user1995421 Jan 24 '13 at 12:48
  • 1
    So you can see that your second number is strictly less than 0.3, even if the rounding during display make it appears as 0.30. – juba Jan 24 '13 at 12:51
  • important are only the first two decimal points, so i will try round() at the point of data input... – user1995421 Jan 24 '13 at 12:52
  • okay, thank you all, i'm sure this will help solve the problem! – user1995421 Jan 24 '13 at 12:54
  • http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – Matthew Lundberg Jan 24 '13 at 15:06

1 Answers1

1

You can use plyr package, and round to multiple of any number.

  x <- round_any(x ,0.01)
agstudy
  • 119,832
  • 17
  • 199
  • 261