Types

Author

Rebecca Barter

Object types

The main types of objects in R are:

  • Numeric type object

  • Character type object, e.g., “California”, “John Doe”

  • Logical type object, TRUE or FALSE

Numeric objects

# Use the class function to determine the class of the value 1
class(1)
[1] "numeric"
# Use the class function to determine the class of the value -5
class(-5)
[1] "numeric"
# Use the class function to determine the class of the value 2.3
class(2.3)
[1] "numeric"
# define y to be 2 * 3 + 1
y <- 2 * 3 + 1
# print y
y
[1] 7

Identify the class of y:

# identify the class of y
class(y)
[1] "numeric"

If your object is “numeric”, you can do mathematical computations with it:

# compute y + 2
y + 2
[1] 9
# compute y cubed
y^3
[1] 343

Character objects

# print out the value "banana"
"banana"
[1] "banana"

Identify the class of the word “banana”

# identify the class of the word "banana"
class("banana")
[1] "character"

Try it without the quotes:

# identify the class of the word banana (without quotes)
class(banana)
Error in eval(expr, envir, enclos): object 'banana' not found

Let’s define a variable with character type

# define a variable, char, that contains the character "banana"
char <- "banana"

Let’s ask what type char has

# identify the class of char
class(char)
[1] "character"

Let’s define a variable that contains “1”

# define a variable called var_one that contains "1" (with quotes)
var_one <- "1"
var_one
[1] "1"

What is the class of var_one?

# identify the class of var_one
one_class <- class(var_one)
one_class
[1] "character"
# Identify the class of the *output* of the class() function?
class(class(1))
[1] "character"

Let’s try to do some math with our character variable

# add char + 1
char <- "hello"
char + 1
Error in char + 1: non-numeric argument to binary operator

Logical objects

The two boolean/logical values are TRUE and FALSE

# print out TRUE 
TRUE
[1] TRUE
# print out FALSE
FALSE
[1] FALSE
# identify the class of TRUE
class(TRUE)
[1] "logical"
# identify the class of FALSE
class(FALSE)
[1] "logical"

Define a logical variable

# define a logical_var containing TRUE
logical_var <- TRUE
logical_var
[1] TRUE
# identify the class of logical_var
class(logical_var)
[1] "logical"

Can we do mathematical operations with logical values?

# Try to subtract 3 from logical_var
logical_var - 3
[1] -2
# Try to multiple FALSE by 4
FALSE * 4
[1] 0

Exercise

Which of the following computations will work?

"TRUE" * 4
"banana" + "apple"
FALSE + 5
TRUE + "TRUE"

Type conversions

Let’s define a variable of each of the three main types.

# define a numeric variable, numeric_var, containing 12.5
numeric_var <- 12.5
# define a character variable, character_var, containing some text
character_var <- "some text"
# define a logical variable, logical_var, containing FALSE
logical_var <- FALSE

Let’s check the class of each variable

# check the class of each of the variables defined above
class(numeric_var)
[1] "numeric"
class(character_var)
[1] "character"
class(logical_var)
[1] "logical"

Converting numeric values to other types

Let’s try to convert the numeric object to a character and logical type

# use the as.character() function to convert numeric_var to a character type
as.character(numeric_var)
[1] "12.5"

Has this modified numeric_var?

numeric_var
[1] 12.5
# use the as.logical() function to convert numeric_var to a logical type
as.logical(numeric_var)
[1] TRUE
# convert -2 to a logical
as.logical(-2)
[1] TRUE
# convert 0.33 to a logical
as.logical(0.33)
[1] TRUE
# convert 0 to a logical
as.logical(0)
[1] FALSE

Numbers can be converted to logical type using as.logical(), but all numbers will be converted to TRUE, except for 0, which is converted to FALSE.

Converting character values to other types

Let’s now try to convert the character object to numeric and logical types

# use the as.numeric() function to convert character_var to a numeric type
as.numeric(character_var)
Warning: NAs introduced by coercion
[1] NA
# use the as.logical() function to convert character_var to a logical type
as.logical(character_var)
[1] NA

Converting character values to other types typically results in a “missing value” NA

# try to do some math with NA
NA + 1
[1] NA

What about when the character is a quoted number?

# try to use as.numeric() to convert "2.3" to a numeric type
as.numeric("2.3")
[1] 2.3
# try to use as.numeric() to convert "2.3 apples" to a numeric type
as.numeric("2.3 apples")
Warning: NAs introduced by coercion
[1] NA

Converting logical values to other types

Let’s see what happens when we convert logical type objects to numeric and character types

# use the as.numeric() function to convert logical_var to a numeric type
as.numeric(logical_var)
[1] 0
# use as.character() function to convert logical_var to a character type
as.character(logical_var)
[1] "FALSE"

Exercise

What will the output of the following code chunks be?

as.numeric("TRUE") + 3
Warning: NAs introduced by coercion
[1] NA
as.character(TRUE + 12)
[1] "13"
as.logical(as.numeric("35"))
[1] TRUE

Logical operations

x <- 2
y <- 4

Let’s ask some questions about x and y

# is x equal to 2?
x == 2
[1] TRUE
# ask is x less than or equal to 1
x <= 1
[1] FALSE
# Is x equal to y?
x == y
[1] FALSE
# Is x *not* equal to y?
x != y
[1] TRUE
!(x == y)
[1] TRUE
# is x strictly greater than y?
x > y
[1] FALSE
# is x greater than or equal to y
x >= y
[1] FALSE
# is x strictly less than y
x < y
[1] TRUE

What about character variables?

x <- "apple"
y <- "banana"
# is x > y
x > y
[1] FALSE
# is x < y
x < y
[1] TRUE
# is x equal to apple
x == "apple"
[1] TRUE
# negate the question
x != "apple"
[1] FALSE
!(x == "apple")
[1] FALSE