This document explores variable classes in R.
The first code chunk creates four vectors with different variable types.
vec_numeric = 5:8
vec_char = c("My", "name", "is", "Jeff")
vec_logical = c(TRUE, TRUE, TRUE, FALSE)
vec_factor = factor(c("male", "male", "female", "female"))
The next code chunk examines the class of each created vector. Note we use collapse = TRUE
to show output within the code chunk rather than in a separate block.
class(vec_numeric)
## [1] "integer"
class(vec_char)
## [1] "character"
class(vec_logical)
## [1] "logical"
class(vec_factor)
## [1] "factor"
Lastly, we have a short chunk that converts a factor variable to a numeric variable.
as.numeric(vec_factor)
## [1] 2 2 1 1