Data Objects in R


Now that we can import data into R, it is important to discuss the many types of data that R handles. For example:

Booleans: Direct binary values: TRUE or FALSE in R.

Integers: Whole numbers or number that can be written without fractional component, represented by a fixed-length block of bits

Characters: fixed length block of bits with special coding.

Strings: Sequence of characters.

Floating Point Numbers: a fraction times an exponent, like 1.34x1071.34×107, however in R you would see 1.34e7.

MissingNaNaN

Figuring out the Data Type

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:

typeof() this function returns the type

is.typ() functions return Booleans for whether the argument is of the type typ

as.typ() 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

Coercing Data Types

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

Equality of Data

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:

What we can see happening here is a problem in the precision of what R has stored for a number.

This can also occur when performing arithmetic operations on values as well.

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() Function

When comparing numbers that we have performed operations on it is better to use the all.equal()function.

Example

all.equal(0.45, 3*0.15)
## [1] TRUE
all.equal(0.4-0.1, 0.5-0.2)
## [1] TRUE