Exercises
Let's return to the num.distinct
function we created earlier. The comment inside the function indicated that we should be careful about using it with a non-integer numeric
input (a float). The problem lies with how unique
handles such inputs. Here's an example:
unique(c(.3, .4 - .1, .5 - .2, .6 - .3, .7 - .4)) # what happened?
Generally, to check for equality between two numeric value (or two numeric columns), we need to be more careful.
.3 == .4 - .1 # returns unexpected result
The right way to check if two real numbers are equal is to see if their difference is below a certain threshold.
abs(.3 - (.4 - .1)) < .0000001 # the right way of doing it
Another more convenient way to check equality between two real numbers is by using the all.equal
function.
all.equal(.3, .4 - .1) # another way of doing it
(1) Use all.equal
to determine if total_amount
is equal to the sum of fare_amount
, extra
, mta_tax
, tip_amount
, tolls_amount
, and improvement_surcharge
.
(2) What are some other ways we could check (not necessarily exact) equality between two numeric variables?