2.1 Assigning values to objects

2.1.1 The assignment operator

The primitive function that most R users are first exposed to is the assignment operator. The assignment operator is used to assign values to named variables. For instance, we can assign a value of 3 to the variable named x using the <- assignment operator.

We can then evaluate the variable by simply typing x at the command line which will return the value of x.

Note that prior to the value returned you’ll see [1] in the command line. This simply implies that the output returned is the first output. Note that you can comment out the content of a line by preceding the line with the hashtag (#) symbol. Any values, symbols, and texts following # will not be evaluated.

Interestingly, R allows for five assignment operators, as shown in Table 2.2. As gain experience using R, you will likely encounter each of these operators sooner or later.

Table 2.2: Assignment operators in R
Assignment operator Operator name
<- Left assignment operator
-> Right assignment operator
- Equals operator
<<- Left deep assignment operator
->> Right deep assignment operator

The original assignment operator in R was <- and has continued to be preferred among many R users. The = assignment operator was added in 2001 primarily because it is the accepted assignment operator in many other languages and beginners to R coming from other languages were so prone to using it. However, R uses = to associate function arguments with values (i.e. f(x = 3) explicitly means to call function f and set the argument x to 3. Consequently, most R programmers prefer to keep = reserved for argument association and use <- for assignment.

The rightward assignment operator returns the same result as their leftward counterparts, they just assign the value in an opposite direction. The deep assignment operators are used to create objects that can be accessed across multiple environments. The deep assignment operators are normally used within functions which we will not get into the details. Exercise caution when using the deep assignment operators as it can lead to unintended (and undesireable) side effects.

My suggestion is to stick with the tried and true <- operator. This is the most widely used assignment operator and is what you will find in all the base R source code.

2.1.2 Naming convention for R objects

With respect to naming objects it’s important to note that R is a case sensitive programming language. Meaning all variables, functions, and objects must be called by their exact spelling. Furthermore, object names can only contain letters and numbers separated by “.” or "_" and cannot begin with a number.

Finally, several distinct conventions exist for naming variables and functions in R. They include the following:

Historically, there’s been no clearly preferred approach among these naming conventions. In some cases, multiple naming conventions can be found within a single package. Bottom line, your naming convention will be driven by your preference but the ultimate goal should be consistency.

A popular approach is to use all lowercase letters with an underscore (_) to separate words within a name. This follows Hadley Wickham’s suggestions in his style guide. Furthermore, variable names should be nouns and function names should be verbs to help distinguish their purpose. Also, refrain from using existing names of functions (i.e. mean, sum, abs). Finally, while it’s good to write code that is concise, an object’s name should be long enough to clearly commmunicate what the object is or what the object does.