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
# Use the class function to determine the class of the value -5
# Use the class function to determine the class of the value 2.3
# define y to be 2 * 3 + 1

# print y

Identify the class of y:

# identify the class of y

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

# compute y + 2

# compute y cubed

Character objects

# print out the value "banana"

Identify the class of the word “banana”

# identify the class of the word "banana"

Try it without the quotes:

# identify the class of the word banana (without quotes)

Let’s define a variable with character type

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

Let’s ask what type char has

# identify the class of char

Let’s define a variable that contains “1”

# define a variable called var_one that contains "1" (with quotes)

What is the class of var_one?

# identify the class of var_one
# Identify the class of the *output* of the class() function?

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

# add char + 1

Logical objects

The two boolean/logical values are TRUE and FALSE

# print out TRUE 

# print out FALSE
# identify the class of TRUE

# identify the class of FALSE

Define a logical variable

# define a logical_var containing TRUE
# identify the class of logical_var

Can we do mathematical operations with logical values?

# Try to subtract 3 from logical_var

# Try to multiply FALSE by 4

Exercise

Which of the following computations will work?

"TRUE" * 4
Error in "TRUE" * 4: non-numeric argument to binary operator
"banana" + "apple"
Error in "banana" + "apple": non-numeric argument to binary operator
FALSE + 5
[1] 5
TRUE + "TRUE"
Error in TRUE + "TRUE": non-numeric argument to binary operator

Type conversions

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

# define a numeric variable, numeric_var, containing 12.5

# define a character variable, character_var, containing some text

# define a logical variable, logical_var, containing FALSE

Let’s check the class of each variable

# check the class of each of the variables defined above

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

Has this modified numeric_var?

# use the as.logical() function to convert numeric_var to a logical type
# convert -2 to a logical

# convert 0.33 to a logical

# convert 0 to a logical

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
# use the as.logical() function to convert character_var to a logical type

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

# try to do some math with NA

What about when the character is a quoted number?

# try to use as.numeric() to convert "2.3" to a numeric type

# try to use as.numeric() to convert "2.3 apples" to a numeric type

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
# use as.character() function to convert logical_var to a character type

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?
# ask is x less than or equal to 1
# Is x equal to y?
# Is x *not* equal to y (in two different ways)?
# is x strictly greater than y?
# is x greater than or equal to y
# is x strictly less than y

What about character variables?

x <- "apple"
y <- "banana"
# is x > y
# is x < y
# is x equal to apple
# negate the question