Back

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:

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:

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:

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.


  1. Ross Ihaka and Robert Gentleman ↩︎

  2. R is licensed under GNU GPL v2 ↩︎

  3. Rick Becker, John Chambers, Doug Dunn, Jean McRae, and Judy Schilling ↩︎

Metadata
PublicationMay 27, 2022
Last editMay 30, 2022
SourcesView source
LicenseCreative CommonsAttribution - Some rights reserved
ContributeSuggest change
Comments