numeric – floating-point numbers (e.g., 3.14, 2, -7.5); most numbers are stored as doubles by defaultinteger – whole numbers (use the L suffix, e.g., 5L)character – textlogical – TRUE or FALSEcomplex – complex numbers (e.g., 2 + 3i)Notes: Vectors in R are homogeneous (one type per vector). Mixing types forces coercion (e.g., c(1, "a") becomes character). Factors (categorical) and data frames are higher‑level structures with classes layered on top of atomic types.
Use these helpers to inspect an object’s type:
typeof(x) — low‑level storage type (e.g., "double", "integer", "character")is.* functions — predicates like is.numeric(), is.character(), is.logical() (return TRUE/FALSE)class(x) — high‑level class (e.g., "factor", "data.frame", or "numeric")Tips: Even whole numbers are "double" unless you explicitly write 5L to make an integer. class() is key for objects with special behavior (e.g., dates, factors, tibbles).
Considerations: as.integer() truncates decimals (e.g., as.integer(2.9) → 2). Integer vs double can matter for memory or exact integer math and when interfacing with low‑level code.
a <- 10.5 # numeric (double)
b <- 5L # integer
typeof(a) # "double"
typeof(b) # "integer"Considerations: Strings are length‑1 character vectors; collections of strings are character vectors. Factors store integers with labels—use as.character() to get text. (Recent R defaults no longer auto‑convert strings to factors.)
Considerations: Watch for NA in logical vectors. Use is.na() and na.rm=TRUE in summaries to handle missing data.
flag <- TRUE
is.logical(flag) # TRUE
5 > 3 # TRUE
"a" == "b" # FALSE
sum(c(TRUE, FALSE, TRUE)) # 2 (TRUE->1, FALSE->0)Considerations: Complex types are less common in general data analysis but invaluable in certain mathematical/statistical tasks.
z <- 2 + 3i
Mod(z) # magnitude
Arg(z) # angle (radians)
Re(z) # real part
Im(z) # imaginary part
Conj(z) # complex conjugateNA with a warning: as.numeric("abc") → NAgrepl() to detect digits) rather than suppressing warnings.num <- "42"
as.numeric(num) # 42
as.character(TRUE) # "TRUE"
as.logical(0) # FALSE
as.integer(2.9) # 2 (truncates)typeof(), class(), and is.*() checksNA) thoughtfully with is.na(), na.omit(), or na.rm=TRUEfactor() / as.factor() when appropriateAsk the AI if you need help understanding or want to dive deeper in any topic