Chapter 5 Working with Data Objects

How do we work with data objects? Often times, we may want to store results of an R calculation for later access.

<- is called an assignment operator and assigns the value on its right to an object named on its left. The equal sign, =, works the same way (in most cases). However, avoid using the equal sign in the off chance you encounter the rare case where it does not work.

5.1 Assignation

Let us assign 3 to the variable m. Be sure to run the code after each command line. You can click “Run” on the upper right-hand side of the Source panel or you can use keystrokes. Keystrokes are usually easier and faster. For PCs, hit “ctrl + enter” and for macs, hit “command + return”.

## [1] 3

Look at the Console panel for calculations or analyses. If you ever need to see what is stored in a given variable, look at the Environment panel. You should see the variable m and its value.

Items after the # symbol are comments and are ignored by the R interpreter. It is a good practice to comment your code. Each line of a comment should begin with the comment symbol, #, and a single space.

Let us create another vector. Let n be a numeric vector created out of a sequence that starts with 13 and ends 19, incremented by 1. We will be using the function c( ). c stands for concatenate (or coerce or combine).

## [1] 13 14 15 16 17 18 19

We can do arithmetic operations on vectors.

## [1] 18 19 20 21 22 23 24

Let w be a character vector made up of the following: Adam, Bob, Charlie, David, Emma, Fiona and George.

## [1] "Adam"    "Bob"     "Charlie" "David"   "Emma"    "Fiona"   "George"

5.2 Retrieving a Value

To retrieve a value in a vector, declare an index inside the square bracket, [ ] operator.

## [1] "David"

To retrieve several values, use the function c( ) inside the square brackets.

## [1] "Bob"     "Charlie" "David"   "Emma"
## [1] "Adam"  "David"

Note that the result of the square bracket operator is another vector.

## [1] "Adam"  "David"

Alternatively, you can look in the Environment panel and look for new_w. You will see the elements of new_w, including its mode.

To determine the length of a particular vector, we use the function length.

## [1] 2

The result shows that there are 2 elements in the vector, new_w.

If the index is negative, the new vector strips the member whose position has the same absolute value as the negative index.

## [1] "Adam"    "Bob"     "Charlie" "Emma"    "Fiona"   "George"
## [1] "Bob"     "Charlie" "Emma"    "Fiona"   "George"
## [1] "Adam"   "Fiona"  "George"

5.3 Error Messages

  1. What happens when the index is out of range? We know that vector w has only 7 entries. Let us extract the 10th entry.

    ## [1] NA
    Trying to extract the 10th entry is impossible and hence the message, NA.

  2. What happens when we ask for an object that does not yet exist in R’s environment? Let us take a look at the result when we call the object, numeral.

    ## Error in eval(expr, envir, enclos): object 'numeral' not found
  3. Is R case sensitive? Suppose, instead of typing lowercase, w, we type uppercase, W. Note that the vector, w (in lowercase) exists but not the vector, W.

    ## Error in eval(expr, envir, enclos): object 'W' not found
    Yes, R is case sensitive!!