Introduction to Quantitative Methods

Operators in R

Operator Description
<- The assignment operator <- is used for assigning a value to a variable. For example, name <- "James Bond", assigns the string "James Bond" to the variable called name.
( ) Round brackets (also known as "parenthesis") are used primarily when calling a function in R. Every function must be called using the round brackets. Some functions need additional information that must be provided to them inside the round brackets. This additional information is called the arguments of a function. For example, the c() function that concatenates or combines elements into a vector needs a sequence of elements as arguments for creating a vector, such as c(1, 3, 5, 7). There are also functions that do not require any arguments, and are simply called using a set of open and closed round brackets. For example, Sys.Date() or getwd().
[ ] The square brackets are used for indexing into a vector, matrix, array, list or dataframe. You can think of the square brackets as marking the edges of a cell, column or row of a table. The square brackets are also called extraction operators as they are used to extract specific elements from a vector or matrix. You can get additional help on extraction operators by typing help(extract) in R.

For example, imagine a matrix m with 7 rows and 5 columns, A, B, C, D, and E. You can extract a single item (or a cell in spreadsheet terminology), an entire row, or an entire column using the extraction operators with the following syntax:

Cell Column Row
> m[4, "C"] > m[,"C"] > m[3,]