Now that we can import data into R, it is important to discuss the many types of data that R handles. For example:
TRUE
or FALSE
in R.1.34e7
.Na
, NaN
, \(\ldots\)With all of these types of data, R, has a built in way to help one determine the type that a certain piece of data is stored as. these consist of the following functions:
typ
() functions return Booleans for whether the argument is of the type typtyp
() functions try to change the argument to type typ
We can see examples of these functions below
typeof(7)
## [1] "double"
is.numeric(7)
## [1] TRUE
We see that 7 is listed as a double. This has to do with the way R stores this data in bits. It is still viewed as a numeric variable though.
is.na(7)
## [1] FALSE
is.na(7/0)
## [1] FALSE
Both of the above values are not considered missing. Even though we cannot calculate 7/0 R will have this as:
7/0
## [1] Inf
If we consider 0/0
though we can see that:
is.na(0/0)
## [1] TRUE
Now if you check what R displays for the answer to this we have
0/0
## [1] NaN
For Character data, this is typically data there it is in quotations:
is.character(7)
## [1] FALSE
is.character("7")
## [1] TRUE
It is important to note that you can turn one data type into another. For example we can turn the number 5/6
into a character:
as.character(5/6)
## [1] "0.833333333333333"
Now we can turn this back to a numeric value:
as.numeric(as.character(5/6))
## [1] 0.8333333
We can then even perform operations on these data after converting them back and forth:
6*as.numeric(as.character(5/6))
## [1] 5
What happens when we check the equality of these values:
5/6 == as.numeric(as.character(5/6))
## [1] FALSE
We might ask what happened here:
Consider the difference between these values. If there were equal this should be 0:
5/6 - as.numeric(as.character(5/6))
## [1] 3.330669e-16
We can see this in other scenarios as well:
0.45 == 3*0.15
## [1] FALSE
0.45-3*0.15
## [1] 5.551115e-17
0.4 - 0.1 == 0.5 - 0.2
## [1] FALSE
all.equal()
FunctionWhen comparing numbers that we have performed operations on it is better to use the all.equal()
function.
all.equal(0.45, 3*0.15)
## [1] TRUE
all.equal(0.4-0.1, 0.5-0.2)
## [1] TRUE