2. Variable Types

In this lesson we will introduce different types of variables.

In this lesson we will introduce different types of variables, including character, numeric, integer, logical.

Types of Variables

Character

my_char <- "Jenny"

Note, we will review functions in a following lesson, but for now just run the below line of code and see what gets returned

class(my_char)
[1] "character"
is.character(my_char)
[1] TRUE

Numeric

my_num1 <- 22 # my favorite number!
my_num1 
[1] 22
my_num2 <- 22.22
my_num2
[1] 22.22
class(my_num1)
[1] "numeric"
class(my_num2)
[1] "numeric"

Integer

my_int1 <- as.integer(22)
class(my_int1)
[1] "integer"
my_int2 <- as.integer(22.22)
class(my_int2)
[1] "integer"
my_int2
[1] 22

Logical

log1 <- TRUE
log2 <- FALSE
class(log1)
[1] "logical"
class(log2)
[1] "logical"
log3 <- "TRUE"
class(log3)
[1] "character"
log4 <- true

Summary

In this lesson, we learned about different types of variables. We specifically focused on 4 of the most common variables:

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
  1. 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.
  1. Now, convert Jennifer’s height to an integer. What number number is returned?
  1. Save your results to a new variable jennifer_int. Do the same steps for Miley and save the result to miley_int.
  1. When we’re dealing with integers, are Miley and Jennifer the same height?
  1. Create three new variables with each woman’s respective full names (first and last). Save your three variables asjennifer_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 🎉