1

I have encountered a funny behaviour when using as.character() on certain numerical values.

Say I have this vector:

test <- seq(-1.5,1,0.02)

Now if I take it as characters, it yields:

as.character(test)
  [1] "-1.5"                "-1.48"               "-1.46"               "-1.44"               "-1.42"               "-1.4"                "-1.38"              
  [8] "-1.36"               "-1.34"               "-1.32"               "-1.3"                "-1.28"               "-1.26"               "-1.24"              
 [15] "-1.22"               "-1.2"                "-1.18"               "-1.16"               "-1.14"               "-1.12"               "-1.1"               
 [22] "-1.08"               "-1.06"               "-1.04"               "-1.02"               "-1"                  "-0.98"               "-0.96"              
 [29] "-0.94"               "-0.92"               "-0.9"                "-0.88"               "-0.86"               "-0.84"               "-0.82"              
 [36] "-0.8"                "-0.78"               "-0.76"               "-0.74"               "-0.72"               "-0.7"                "-0.68"              
 [43] "-0.66"               "-0.64"               "-0.62"               "-0.6"                "-0.58"               "-0.56"               "-0.54"              
 [50] "-0.52"               "-0.5"                "-0.48"               "-0.46"               "-0.44"               "-0.42"               "-0.4"               
 [57] "-0.38"               "-0.36"               "-0.34"               "-0.32"               "-0.3"                "-0.28"               "-0.26"              
 [64] "-0.24"               "-0.22"               "-0.2"                "-0.18"               "-0.16"               "-0.14"               "-0.12"              
 [71] "-0.0999999999999999" "-0.0800000000000001" "-0.0600000000000001" "-0.04"               "-0.02"               "0"                   "0.02"               
 [78] "0.04"                "0.0600000000000001"  "0.0800000000000001"  "0.1"                 "0.12"                "0.14"                "0.16"               
 [85] "0.18"                "0.2"                 "0.22"                "0.24"                "0.26"                "0.28"                "0.3"                
 [92] "0.32"                "0.34"                "0.36"                "0.38"                "0.4"                 "0.42"                "0.44"               
 [99] "0.46"                "0.48"                "0.5"                 "0.52"                "0.54"                "0.56"                "0.58"               
[106] "0.6"                 "0.62"                "0.64"                "0.66"                "0.68"                "0.7"                 "0.72"               
[113] "0.74"                "0.76"                "0.78"                "0.8"                 "0.82"                "0.84"                "0.86"               
[120] "0.88"                "0.9"                 "0.92"                "0.94"                "0.96"                "0.98"                "1"       

So there are some values that get additionnal digits, that I would rather not have there.

I know that this is due to how numbers are stored in R, but I would still like to have a string vector that corresponds to the numbers I have in the first vector (in order to access certain rows and columns in a matrix, I need the names to be identical).

Taking instead

as.character(round(test,digits=2))

solves my problem, but I'm not entirely sure it is fool-proof. Does anyone have advice? Or is there something I have overlooked?

Thanks!

Delphine
  • 31
  • 5
  • 2
    Try `print(seq(-1.5,1,0.02), digits = 22)` [These are floating points](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal)- they aren't funny and they actually exsist – David Arenburg Mar 16 '16 at 15:19
  • For 1 on 1 conversion to character class, we can use `format(test)` – mtoto Mar 16 '16 at 15:20
  • 2
    mtoto `trimws(format(test))` to account for the extra space – Pierre L Mar 16 '16 at 15:36
  • Crazy suggestion: Don't use floating point numbers as names. – Roland Mar 16 '16 at 15:41
  • @DavidArenburg in the example you give, almost all of them have floating points, but only a few of them are revealed by as.character. How does as.character choose? It doesn't seem to be because of rounding because then a lot more would appear that have "larger" Floating Points values. – Delphine Mar 16 '16 at 15:55
  • @Roland What would you suggest as dimnames for grids in space and in time? Thank you all for the feedback! – Delphine Mar 16 '16 at 15:56
  • @Delphine This looks like a regular grid. Just use column and row numbers and save the coordinates separately, maybe in an attribute. (I would also assume that the spatial packages offer some better solutions, but that's not my area of expertise.) – Roland Mar 16 '16 at 16:15

0 Answers0