In this lesson we will introduce different types of variables, including character, numeric, integer, logical.
Types of Variables
Character
A character is a type of class that contains a string of characters. For example, a name is a string of characters (or letters).
In the example below, Iâve saved my name âJennyâ to a variable Iâve called âmy_charâ.
- To check what type of variable you have, you can use the
class()
function
- All we have to do is insert our variable (my_char) within the parentheses of our function
Note, we will review functions in a following lesson, but for now just run the below line of code and see what gets returned
As you can see, R returned the word âcharacterâ as we may have expected
We can also use the is.character()
function to check if our variable is a character. This function will return either TRUE or FALSE
Numeric
- Numeric is a type of class that contains whole numbers or numbers with decimals
- Letâs test out a couple of examples
- We will first assign a number to a new variable (my_num1 or my_num2)
- And then weâll ask R to print (or return) the value for us
my_num1 <- 22 # my favorite number!
my_num1
[1] 22
[1] 22.22
- Letâs check the classes of the variables weâve just created
[1] "numeric"
[1] "numeric"
- We can see that both of our variables are numeric
Integer
- An integer is a type of class that contains only whole numbers
- Of course, this is similar to the numeric class, but without decimals. So how does R know if a whole number is numeric or an integer?
- By default, R will assign any whole number to the numeric class. We observed this above when we saw the class of the number 22 was numeric
- However, as weâll see below, you can convert a numeric class to an integer using the
as.integer()
function
- But, what happens if we try to convert a number with a decimal to an integer?
- This works! It has successfully converted 22.22 to an integer
- However, it is important to note that 22.22 was forced to become a whole number
- So, if we look at my_int2, it is now the whole number 22 rather than the decimal 22.22
Logical
- Logical is a special type of class that contains only two possible values: TRUE or FALSE
- Please note that the logical values must be written in all caps with no quotes
log1 <- TRUE
log2 <- FALSE
- Once again, weâll check the classes of the variables weâve just created
[1] "logical"
[1] "logical"
- What happens if we try the following?
log3 <- "TRUE"
class(log3)
[1] "character"
- Because TRUE is in ââ here, by default R assigns this to the character class
- This example doesnât even run! R gives us an error here saying âobject âtrueâ not foundâ
Summary
In this lesson, we learned about different types of variables. We specifically focused on 4 of the most common variables:
- Character: list of character strings
- Numeric: whole numbers OR numbers with decimals
- Integer: Only whole numbers
- Logical: TRUE or FALSE
Exercises
In this exercise, weâll return to our three fabulous female artists: Jennifer Lopez, Taylor Swift, and Miley Cyrus. In the exercises in lesson we, we compared their heights, which we can see below.
Remember to run the code before moving on.
jennifer <- 64.57
taylor <- 70.87
miley <- 64.96
- What kind of variables are we dealing with here? Are they characters, numeric, integers, or logical? Once you think you know the answer, in the R Chunk below, use the
class()
function to see if youâre right. Test it out with any or all of the artists.
- Now, convert Jenniferâs height to an integer. What number number is returned?
- Save your results to a new variable
jennifer_int
. Do the same steps for Miley and save the result to miley_int
.
- When weâre dealing with integers, are Miley and Jennifer the same height?
- Create three new variables with each womanâs respective full names (first and last). Save your three variables as
jennifer_name
, taylor_name
, and miley_name
.
Hint: ff you get an error that says âobject not foundâ, return to the very first R Chunk in this lesson and see how I saved my name.
What type of variables are these? Test it out with any or all of the artists.
THE END đ