3b What happens if we try converting to integer before rounding?

So converting variables from one type to another is also known as casting variables in computer science.
So we are going to cast floating point ie num to integer or int.
I’m really telling you this so you know what to try and google when working in other programming languages.
In reality my definition above is quite poor and for some languages there is a difference between casting and converting variables.
But the difference can be subtle and I’m not going to go over it here in more detail.

Back to the question at hand to explore converting to integer first reload the data.
clean_data=read.csv("ncdc_galesburg_daily_clean.csv",stringsAsFactors=FALSE)
clean_data$Date=as.Date(clean_data$Date)
## then enter
table(as.integer(clean_data$TMAX))
#this returns
-11 -9 -7 -5 -4 -2 0 1 3 5 6 8 10 12 14 15
4 5 2 4 1 9 8 16 30 28 44 54 81 103 50 139
17 19 21 23 24 26 28 30 32 33 35 37 39 41 42 44
136 198 205 99 276 342 436 520 306 657 678 634 606 277 621 593
46 48 50 51 53 55 57 59 60 62 64 66 68 69 71 73
554 498 263 515 549 553 580 296 593 579 590 616 306 758 733 806
75 77 78 80 82 84 86 87 89 91 93 95 96 98 100 102
855 396 967 1080 1113 1066 436 812 638 450 276 79 104 37 21 3

As you can see when we try converting to integer before rounding we lose daily average temperatures.
For example where are -10,-8,-6,-3,-1,2,4,7,9 ? Now let’s try rounding before we convert.
table(as.integer(round(clean_data$TMAX)))
#this returns
-12 -11 -10 -9 -8 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
2 2 3 2 2 1 3 1 3 6 4 4 9 7 8 22 28 20 24 26
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
28 50 31 47 56 50 78 61 74 62 92 106 93 112 99 135 141 165 177 221
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
215 261 259 306 334 323 353 325 320 314 289 317 277 307 314 325 268 282 272 252
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
246 263 232 283 281 268 292 261 269 311 296 306 287 300 279 273 317 278 338 306
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
397 361 361 372 413 393 445 410 396 504 463 569 511 546 567 519 547 436 425 387
89 90 91 92 93 94 95 96 97 98 99 100 101 102
339 299 240 210 151 125 79 67 37 17 20 17 4 3

This is what we would expect.
Back to Step 3.

Leave a Reply