R R

R: Variables & Data Types

What you will learn

R uses <- for assignment (though = also works). It is dynamically typed and was designed by statisticians, so some conventions differ from general-purpose languages.

name <- "Alice"      # character (string)
age <- 25L           # integer (L suffix forces integer)
height <- 5.6        # numeric (double by default)
is_student <- TRUE   # logical (uppercase!)

Data types in R

Type Example Notes
numeric 3.14, -2.5 Default for numbers — double precision
integer 42L Uses L suffix to distinguish from numeric
character "hello", 'world' Strings (single or double quotes)
logical TRUE, FALSE Must be uppercase (T and F are shortcuts but not recommended)
factor factor(c("A","B")) Categorical data with levels
NULL NULL Absence of any value (like null)
NA NA Missing value (different from NULL)

Key differences from other languages

  • R uses 1-based indexing, not 0-based.
  • <- is traditional for assignment (Alt+- in RStudio is a shortcut).
  • TRUE / FALSE must be uppercase — true / false are just variable names.
  • R has NA (missing data) separate from NULL (undefined).

Common mistakes

  • Using true instead of TRUE — R is case-sensitive and won't recognize lowercase.
  • Using = for assignment in function arguments — = is for named arguments; <- is for assignment outside.
  • Forgetting L suffix and getting a numeric (double) instead of integer.
  • Confusing NA (missing, still exists) with NULL (does not exist at all).

Quick check below!