# 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”.

m <- 3  # Assigns 3 to m
m   # Extracts the value m
## [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).

n <- c(13:19)
n   # Extracts the vector, n
## [1] 13 14 15 16 17 18 19

We can do arithmetic operations on vectors.

# Add 5 to each entry in the vector, n
n + 5   
## [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.

w <- c("Adam", "Bob", "Charlie", "David", "Emma", "Fiona", "George")
w
## [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.

# Extracts the 4th entry in vector w
w[4]    
## [1] "David"

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

# Displays 2nd to 5th entries in vector w
w[c(2:5)]
## [1] "Bob"     "Charlie" "David"   "Emma"
 # Displays 1st and 4th entries in vector w
w[c(1,4)]
## [1] "Adam"  "David"

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

# Assigns the 1st and 4th item in vector w to a new vector called new_w
new_w <- w[c(1,4)]
new_w
## [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.

length(new_w)
## [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.

# Strips the 4th entry of vector w
w[-4]   
## [1] "Adam"    "Bob"     "Charlie" "Emma"    "Fiona"   "George"
# Strips the 1st and 4th entries of vector w
w[c(-1, -4)] 
## [1] "Bob"     "Charlie" "Emma"    "Fiona"   "George"
# Strips the 2nd to the 5th entries of vector w
w[c(-2:-5)]
## [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.

w[10]
## [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.

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.

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