Classes

All variables have a class. Variables also have modes and storage types, but those are legacy and don't worry about them.

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# Find a variable's class
class(TRUE)
[1] "logical"

R has three classes of numbers

# Numeric
class(sqrt(3))
[1] "numeric"
# Complex
class(3i)
[1] "complex"
# Integer (add L to make a number an integer)
class(3L)
[1] "integer"
# Integer
class(3:33)
[1] "integer"

R Also Has Other Classes

# Characters (Strings, like text)
class(c("Arizona", "Maryland"))
[1] "character"
# Factors (like unordered categories)
class(factor(c("male", "female")))
[1] "factor"
# Factors have both values (i.e. a label) and a level (i.e. a numeric ID number)
gender <- factor(c("male", "female"))
# View the values
levels(gender)
[1] "female" "male"
# View the number of levels
nlevels(gender)
[1] 2
# View the levels of each element of a factor
as.integer(gender)
[1] 2 1
# View the values of a factor as character strings
as.character(gender)
[1] "male"   "female"