Intro to R
R is an interpreted programming language for statistical computation and graphics introduced in early 1990s by statisticians1 from the University of Auckland.
R is an open source2 implementation of S which itself was designed around 1976 by engineers3 working at Bell Labs.
Objects and types #
Unlike most other programming languages, R doesn’t provide the user with a direct access to the computer’s memory through variables, but rather to a set of objects (known as SEXPREC), which are specialized C data structures to be filled in values.
The type of the objects (SEXPTYPE) refers to its specialization. Most common types include:
- Function:
closure - Recursive vector:
listwhose elements can be any objects - Atomic vector
logicalaka Booleannumericintegerdoubleaka real
character
Objects are referred to through symbols (names pointing to the object) which are objects themselves but they can also be anonymous.
Closures #
An R function is called a closure because it captures the symbols
(bindings) that live in the environment it is evaluated in.
This is an important feature of R called lexical scoping.
A closure is a list of 3 elements:
formalarguments: list of named symbols with possible default valuebodyenvironment: providing lexical scoping, immutable bindings- frame: list of symbol=value pairs
- enclosure: pointer to enclosing/parent environment rooted in
emptyenv()which is the direct parent ofbaseenv()where lives the base package which is the parent of the global environment, the root of the user workspace
Functions are called for either or both
computational results (value which can be assigned to a symbol)
and side effects (print, plot as well as setters class<-).
Function calls’ evaluation use exact, partial and positional arguments matching.
Operators are just function calls assigned to non standard symbols.
11 + 2
2`+`(1, 2)
## [1] 3
## [1] 3
Functions that are part of the language are called builtin (.Primitive)
vs. interpreted functions.
Attributes and method dispatch #
Objects can be extended with attributes.
The class attribute controls R’s elaborate class system (aka S3).
A class is a character vector of class names the object inherits from.
Common classes include:
factor: integer atomic vector with alevelsattribute for representing categorical dataarrays: atomic vector withdim/dim.namesattributesmatrixspecial case ofarrayswith 2 dimensions
data.frame: list whose element must be of equal length withrow.namesattribute for representing tabular data
A method is a variation of a generic function dispatched by the class of the object passed to it.
Some special methods known as group methods, allows for double dispatch. They are used for comparison and arithmetic operations.
Indexing vectors #
Indexing a vector
[[[$
with index or a name in the names attribute character vector.
Comments