29  Variable assigment

Trinite

sary_mainitirano.jpeg, foza orana.mp3, Malok'ila_1.mp4. Beyond clicking these files on a computer, consider how they actually work.

Suppose a filename is changed (renamed), for example: sary_maintirano.jpeg to sary_paris.jpeg. When opened, the image remains the photo taken in Maintirano. In other words, a filename is something symbolic—a label created by the user to indicate the content. It can be frustrating to find a file with no indication of its contents unless it is opened.

Fundamentally, file content is a sequence of 0s and 1s stored in the computer’s memory. To access this sequence, the address (the location of that sequence) is required. Figuratively, the filename is like the name of a house (e.g., Villa Fanomezana); the name can change, but the physical house and its address generally remain the same.

In programming, an assignment consists of binding three elements: creating an object (building a house) at a specific location (the address) and assigning it a chosen label. While one can choose what object to create and what to name it, the address itself (where it is placed in computer memory) is determined by the system—which is ultimately for the best.

Formal

In programming language design, assignment is defined as a mechanism that dynamically changes the binding between a storage location and a value. It introduces the foundational concepts of L-values (location/address) and R-values (read/data value).

In R, the syntax is: variablename <- object which reads: put the object in memory and assign it a name. The assignment operator essentially creates a new variable name, allocates an address, and stores the created object there.

An object can be almost anything; in fact, the rest of this section covers the different types of fundamental objects, from Chapter 30 to Chapter 39.

Examples

  1. Assignment and console output

    # Store the number(object) 2 in variable called a.
    a <- 2

    In the first example above, the number (object/value) 2 is stored in a variable called a. Running that line in R produces no output in the console, because the instruction is to create the object, not to evaluate it.

    # this will display the value stored in a
    a
    [1] 2
  2. Adding numbers two numbers using their names: Instead of calculating 2 + 3 directly, 2 can be stored in a and 3 in b. Then, a + b can be added and stored in c. Notice that variable names are symbolic.

    # Creating two variables to store the value 2 and 3
    a <- 2
    
    b <- 3
    
    # Storing the result of the calculation
    c <- a + b
    
    # Display the value in c
    c
    [1] 5
  3. Swapping values: A very instructive exercise for understanding assignment is swapping the contents of two variables.

    # Defining the two variables
    variable1 <- 1
    variable2 <- 20
    
    # Swapping elements between box
    # Put the value of variable1 to variable2
    variable2 <- variable1
    # Put the value of variable2 to variable1
    variable1 <- variable2
    # Print the value of variable1
    variable1
    [1] 1
    # Print the value of variable2
    variable2
    [1] 1

    It does not work!!! the value inside variable1 becomes 1 (no swap occurs), and variable2 also becomes 1. The solution to this exercies is similar to swapping juice in a cup and coffee in a glass: to swap the contents, one must first transfer one of the beverages into a third container before proceeding.

    # Creating two variables
    variable1 <- 1
    variable2 <- 20
    
    # Swapping elements using a thrid temproray variable
    temporary <- variable1
    variable1 <- variable2
    variable2 <- temporary
    # Print the values in variable1
    
    variable1
    [1] 20
    # Print the values in variable2
    variable2
    [1] 1

    The values are swapped.

Tips and warnings

  • The default shortcut for <- in RStudio on Windows is Alt + - and Option + - on Mac.

  • The three variables below are all different. Object names are case sensitive and typo sensitive.

    • my_variable <- 2,
    • my_Variable <- 2,
    • my_varable <- 2
  • The most common error in programming is shown below Figure 29.1. While it can cause panic, it is the easiest to solve, and the main culprits are:

    • Typos: case sensitivity, spelling etc.
    • Unassigned variables, where code was typed in the script but not executed, so the object is not stored in the environment
    Figure 29.1: Object not found
  • In R, = can also be used for object assignment (a = 2 works as well). However, <- is preferred, reserving = for specific contexts such as passing arguments inside functions.

  • The rightward assignment operator -> is also supported (2 -> a).

  • Creating evocative variable names is a good habit so the content can easily be inferred.

  • Variable naming conventions:

    • No spaces or special characters (%, $, etc.)
    • Cannot start with a number or an underscore
    • Multi-word conventions:
      • TelmaNumber and MadacomNumber (PascalCase)
      • telmaNumber and madacomNumber (camelCase)
      • telma_number and madacom_number (snake_case)
      • TELMA_NUMBER and MADACOM_NUMBER (screaming snake_case)
      • telma.number and madacom.number (informal dot.case)
  • After evaluation, the top-right panel shows all created objects, along with their names and values.

Figure 29.2: All the variables that was evaluated in the console appear on the right with their values

Notation/Semantic/Terminology

The word variable has now appeared in mathematics, statistics (Chapter 16), and in R. Does it mean the same thing? Fundamentally, yes. It is a combination of notation (variable name) and a value. A variable is symbolic in mathematics (think of \(x\), \(f(x)\), or \(y\)) because it is understood that it could take any value, it varies. In statistics, a variable also takes different values for different individuals in the population, although the variable is often thought of as the realized values and thus a sequence of values. The variable in programming embraces two perspectives: (1) it can be symbolic as in mathematics (which is why many examples will use names like \(a\), \(b\), \(c\), etc.) to store a value that could be replaced at any time, and (2) it stores the sequences of characteristics of a population as vectors (Chapter 35) and data frames (Chapter 38) that, once recorded, will not change. Variable names becomes height or age. There are more nuances if one digs deeper into a more rigorous definition, but intuitively, this distinction suffices.