# ============================================================
# Data Types in R
# R Foundations — Rverse Analytics
# https://rverseanalytics.com/learn/data-types
# ============================================================

# Data types in R
x <- 42L          # integer
y <- 3.14         # double (numeric)
name <- "Rverse"  # character
ok <- TRUE        # logical
class(x); class(y); class(name); class(ok)
# Check a type, or convert (coerce) it
is.numeric(y)
is.character(name)
as.integer("10") + 5
as.numeric("3.5") * 2
# Logicals are numbers underneath
sum(c(TRUE, FALSE, TRUE, TRUE))    # counts the TRUEs
mean(c(TRUE, FALSE, TRUE, TRUE))   # proportion that are TRUE
# A factor stores categories with a fixed set of levels
grp <- factor(c("low", "high", "low", "mid"), levels = c("low", "mid", "high"))
grp
as.integer(grp)   # each category is stored as a code
