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/FALSEmust be uppercase —true/falseare just variable names.- R has
NA(missing data) separate fromNULL(undefined).
Common mistakes
- Using
trueinstead ofTRUE— R is case-sensitive and won't recognize lowercase. - Using
=for assignment in function arguments —=is for named arguments;<-is for assignment outside. - Forgetting
Lsuffix and getting a numeric (double) instead of integer. - Confusing
NA(missing, still exists) withNULL(does not exist at all).
Quick check below!