R variables are bindings in an environment—assignment uses <- (preferred) or =. Types are attached to objects; vectors are homogeneous (one type per vector), unlike mixed Python lists.
Common atomic types
numeric— doubles by default (integers exist but are less common)character— text stringslogical—TRUE,FALSE,NAinteger— suffixL, e.g.5L
Assignment and class()
count <- 10
ratio <- 0.75
name <- "Ada"
ok <- TRUE
print(class(count))
print(typeof(name))
Use class() and typeof() to inspect objects—compare with type() in Python.
Important interview questions and answers
- Q: Why <- over =?
A: Both assign; <- is idiomatic and avoids confusion with function arguments using =. - Q: Are R vectors homogeneous?
A: Yes—c(1, "a") coerces to character; use lists for mixed types.
Self-check
- What does class(3.14) return?
- Can one vector hold numbers and strings without coercion?
Pitfall: Vectors are homogeneous—c(1, "a") coerces to character; use lists for mixed types.
Interview prep
- <- vs =?
<-is idiomatic for assignment;=works but can confuse with named arguments.