1.1 Basics of R
1.1.1 Help
Help can be accessed via help or simply ?. If you do not know what to query, use ?? in front of a string, delimited by captions " " as in ??"Cholesky decomposition". Help is your best friend if you don’t know what a function does, what are its arguments, etc.
1.1.2 Basic commands
Basic R commands are fairly intuitive, especially if you want to use R as a calculator.
Elementary functions such as sum, min, max, sqrt, log, exp, etc., are self-explanatory.
Some unconventional features of the language:
- Use
<-to assign to a variable, and=for matching arguments inside functions - Indexing in R starts at 1, not zero.
- Most functions in R are vectorized, so avoid loops as much as possible.
- Integers are obtained by appending
Lto the number, so2Lis an integer and2a double.
Besides integers and doubles, the common types are
- logicals (TRUE and FALSE);
- null pointers (NULL), which can be assigned to arguments;
- missing values, namely NA or NaN. These can also be obtained a result of invalid mathematical operations such as log(-2).
The above illustrates a caveat of R: invalid calls will often returns something rather than an error. It is therefore good practice to check that the output is sensical.
1.1.3 Linear algebra in R
R is an object oriented language, and the basic elements in R are (column) vector. Below is a glossary with some useful commands for performing basic manipulation of vectors and matrix operations:
cas in _c_oncatenates creates a vectorcbind(rbind) binds column (row) vectorsmatrixandvectorare constructorsdiagcreates a diagonal matrix (by default with ones)tis the function for transposesolveperforms matrix inversion%*%is matrix multiplication,*is element-wise multiplicationcrossprod(A, B)calculates the cross-product \(\mathbf{A}^\top\mathbf{B}\),t(A) %*% B, of two matricesAandB.eigen/chol/qr/svdperform respectively an eigendecomposition/Cholesky/QR/singular value decomposition of a matrixrepcreates a vector of duplicates,seqa sequence. For integers \(i\), \(j\) with \(i<j\),i:jgenerates the sequence \(i, i+1, \ldots, j-1, j\).
Subsetting is fairly intuitive and general; you can use vectors, logical statements. For example, if x is a vector,
then
x[2]returns the second elementx[-2]returns all but the second elementx[1:5]returns the first five elementsx[(length(x) - 5):length(x)]returns the last five elementsx[c(1, 2, 4)]returns the first, second and fourth elementx[x > 3]return any element greater than 3. Possibly an empty vector of length zero!x[ x < -2 | x > 2]multiple logical conditions.which(x == max(x))index of elements satisfying a logical condition.
For a matrix x, subsetting now involves dimensions: [1,2] returns the element in the first row, second column. x[,2] will return all of the rows, but only the second column. For lists, you can use [[ for subsetting by index or the $ sign by names.
1.1.4 Packages
The great strength of R comes from its contributed libraries (called packages), which contain functions and datasets provided by third parties. Some of these (base, stats, graphics, etc.) are loaded by default whenever you open a session.
To install a package from CRAN, use install.packages("package"), replacing package by the package name. Once installed, packages can be loaded using library(package); all the functions in package will be available in the environment.
There are drawbacks to loading packages: if an object with the same name from another package is already present in your environment, it will be hidden. Use the double-colon operator :: to access a single object from an installed package (package::object).