R Data Types

Basic Data Types

  • numeric – floating-point numbers (e.g., 3.14, 2, -7.5); most numbers are stored as doubles by default
  • integer – whole numbers (use the L suffix, e.g., 5L)
  • character – text
  • logicalTRUE or FALSE
  • complex – 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.

Checking Data 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).

Loading...
Output:

Numeric and Integer

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"

Character (Strings)

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.)

Loading...
Output:

Logical (Boolean)

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)

Complex Numbers

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 conjugate

Type Conversion

  • Pitfalls: Non‑numeric strings become NA with a warning: as.numeric("abc")NA
  • Prefer validating first (e.g., grepl() 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)

Best Practices

  • Validate input types early using typeof(), class(), and is.*() checks
  • Be explicit with conversions before arithmetic or comparisons to avoid unintended coercion
  • Keep vector types consistent when building data frames (avoid mixing numbers and text in the same column)
  • Handle missing data (NA) thoughtfully with is.na(), na.omit(), or na.rm=TRUE
  • Prefer character over factor unless you need categorical semantics; convert deliberately with factor() / as.factor() when appropriate

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic