Logis and Doubles and Strings, oh my!
When we were making some vectors in the last lesson, you might have noticed a word appear in the environment panel between the name of the vector, and the values it contained:
You can see the data type of your vectors in the environment panel!
These are the types of data that make up the vector, and determine the properties of the data, and what kind of manipulations you can perform on it.
In R, all data objects have a data type. In this lesson we are going to have look at 5 important data types, which from most simple to most complex are:
- Complex Numbers
- and Strings
Let's go into a little detail about these different data types.
The simplest of data types you can encounter in R are the logicals. Logical type data can take one of two values, either
FALSE.We have already encountered logicals in the last chapter, when performing comparisons.
> 5 >= 3
> c(1, 2, 3, 4, 5) >= c(5, 4, 3, 2, 1)
 FALSE FALSE TRUE TRUE TRUE
If you're feeling lazy, logicals can also be defined just by using
While it's a little unusual to assign logicals as variables, it is quite common to receive them as outputs from tests or functions you might run and use them in conditional
if elsetype statements (more on this in a later lesson).
Moving one step up in complexity from logicals, we get to integers, which are used to represent, well, integers? To differentiate themselves from the more common double type, integers in R are written as a number followed by a capital
L. Let's explore this difference using the
typeof()function, which will return the data type of an object.
# not an integer
# is an integer
For the most part, R will automatically handle and swap between integers and doubles as necessary, and the difference is generally not something you generally need to worry about.
> 1L + 1.5 # although we started with an integer
[2.5] # R knows we want a double at the other end
That said, integers do serve an important purpose when R passes code to software written in
FORTRAN,but this is well beyond the scope of this course. At the very least, now you know integers exist!
In most cases, your numeric R data is going to be stored using the double data type. Doubles are used to represent any the real numbers you might want to use.
The term double is short for double-precision number, meaning they can store twice the information as our integers. As well as storing decimal values, this means you can store much bigger numbers with doubles than integers.
# the largest possible integer value...
# is much smaller than the largest possible double
 1.797693e+308 # remember, e+308 means x 10 to the power of 308
However, they also take up more space in storage than integers.
Moving up in complexity from our doubles, we get to imaginary and complex numbers. In R, a complex number are written as
<real> + <imaginary>i.
# R will try to stay in the real world
In sqrt(-1) : NaNs produced
# unless you explicitly tell it not to
> sqrt(-1 + 0i)
 0 + 1i
For some of us it would be pretty rare to need to delve into the world of complex numbers, but R has full support for complex algebra if that's what takes your fancy.
Characters are how R deals with data that comes in the shape of words, rather than numbers. Characters can consist of any characters you want, as long as they're wrapped up inside a pair of double
# This is a string of letters
# And a string of numbers
# And a string of words
> "string of words"
 "string of words"
A common slipping place for new R users is dealing with strings of numbers. While it's perfectly reasonable to do something like
> 10 + 22.5
Trying to do the same thing with numbers in characters will land you in some strife!
> "10" + "22.5"
Error in "10" + "22.5" : non-numeric argument to binary operator
Just because your data starts as one type, that doesn't mean it needs to stay that way! To change data from one type to another, we can use the inbuilt type coercion functions. To coerce your data into:
- A logical, use
- An integer, use
- A double, use
- A complex number, use
- Or a string, use
While you can always coerce a less complex data type to a more complex one, the reverse isn't always true.
 1 # no problemo
 "10" # all good here too
> as.numeric("scooby snacks")
NAs introduced by coercion # ruh roh!
Now that we've explored the different types of data in R, here are a few challenges to test what you've learned.
# What is the data type of each of the following objects:
> sqrt(-1 +0i)
> typeof(sqrt(-1 +0i))
# Which of these coercions work? What do they produce?
> as.numeric("scooby snacks")
> as.logical("Hello!") # Coercion from string to logical is not defined
> as.numeric("scooby snacks") # Coercion from string to double is not defined
Warning message: NAs introduced by coercion
# What is the output of each of these lines of code? Why?
> "1" + "5"
> 1 + 7
> TRUE + FALSE
 NaN # R will try to stay in the real world try sqrt(-1 +0i) instead
Warning message: In sqrt(-1) : NaNs produced
> "1" + "5" # You can not add 2 characters
Error in "1" + "5" : non-numeric argument to binary operator
> 1 + 7
> TRUE + FALSE # They are coerced as numbers as 1 and 0 respectively