1 First Contact with Vectors

In order to enjoy and exploit R as a computational tool, one of the first things you need to learn is about the objects R provides to handle data. The formal name for these programming elements is data objects also known as data structures. They form the ecosystem of data containers that we can use to handle various types of data sets, and be able to operate with them in different forms.

I’m going to use financial math examples as an excuse to introduce and explain the material. I’ve found that having a common theme helps avoiding falling into the “teaching trap” of presenting isolated examples in a vacuum.

1.1 Motivation: Compound Interest

I would like to ask you if you have any of the following accounts:

  • Savings account?

  • Retirement account?

  • Brokerage account?

Don’t worry if you don’t have any of these accounts. I certainly didn’t have any of those accounts until I started my first job right after I finished college.

Anyway, let’s consider a hypothetical scenario in which you have $1000, and you decide to deposit them in a savings account that pays you an annual interest rate of 2%. Assuming that you leave that money in the savings account, an important question to ask is:

How much money will you have in your savings account one year from now?

The answer to this question is given by the compound interest formula:

\[ \text{deposit} + \text{annual paid interest} = \text{amount in one year} \]

In this example, you deposit $1000, and the bank pays you 2% of $1000 = $20, 12 months from now.

In mathematical terms, we can write the following equation to calculate the amount that you should expect to have in your savings account within a year:

\[ 1000 + 1000 (0.02) = 1000 \times (1 + 0.02) = 1020 \]

You can confirm this by running the following R command:

# in one year
1000 * (1.02)
> [1] 1020

Now, if you leave the $1020 in the savings account for one more year, assuming that the bank keeps paying you a 2% annual return, how much money will you have at the end of the second year?

Well, all you have to do is repeat the same computation, this time by letting the $1020—accumulated during the first year—compound for one more year:

\[ 1020 + 1020 (0.02) = 1020 \times (1 + 0.02) = 1040.40 \]

which in R can be computed as:

# in two years
1020 * (1.02)
> [1] 1040.4

How much money will you have at the end of three years? Again, take the amount saved at the end of year 2, and compund it for one more year:

\[ 1040.4 + 1040.4 (0.02) = 1040 \times (1 + 0.02) = 1061.208 \]

We can confirm this in R by running the following command:

# in three years
1040.40 * (1.02)
> [1] 1061.208

1.1.1 Comments

One thing to note in all the previous commands is the use of the informative text preceded by the pound or hash # symbol, for example: # in three years. This kind of text is not a command but rather a comment.

# this is a comment
# and so is this

# in four years
1061.208 * (1.02)
> [1] 1082.432

All programming languages use a set of characters to indicate that a specific part or lines of code are comments, that is, things that are not to be executed. R uses the # symbol to specify comments. Any code to the right of # will not be executed by R.

1.1.2 Creating Objects

Often, it will be more convenient to create objects , also referred to as variables, that store both input and output values. To do this, type the name of the object, followed by the equals sign =, followed by the assigned value. For example, you can create an object d for the initial deposit of $1000, and then inspect the object by typing its name:

# deposit 1000
d = 1000
d
> [1] 1000

Alternatively, you can also use the arrow operator <-, technically known as the assignment operator in R. This operator consists of the left-angle bracket (i.e. the less-than symbol) and the dash (i.e. hyphen character).

# interest rate of 2%
r <- 0.02
r
> [1] 0.02

1.1.3 Assignment Statements

All R statements where you create objects are known as “assignments”, and they have this form:

object <- value

# equivalent to
object = value

this means you assign a value to a given object; you can read the previous assignment when we created the interest rate as “r gets 0.02”.

Here are more assignments for each of the savings amounts at the end of years 1, 2, and 3:

# amounts at the end of years 1, 2, and 3
a1 = d * (1 + r)
a2 = a1 * (1 + r)
a3 = a2 * (1 + r)
a3
> [1] 1061.208

1.1.4 Case Sensitive

Recall that R is case sensitive. This means that a1 is not the same as A1. Similarly, money is not the same as Money or MONEY

# case sensitive
money = 10
Money = 100
MONEY = 1000

money + Money
> [1] 110
MONEY - Money
> [1] 900

1.1.5 Use Descriptive Names

While the names of objects such as d, r, a1, etc, are good for a computer, they can be a bit cryptic for a human being. Right now you may not have an issue understanding what those names refer to. You may even find these names to be quite convenient: they are short and easy to type. Moreover, they match the algebraic notation used in the compound interest formula. What’s not to like about them?

Well, the issue is that this kind of names are too short. Mathematically there’s nothing wrong with them. Computationally, from the point of view of the programming language (R), there is also nothing inherently bad about them. However, from the human (i.e. the reader or the code reviewer) standpoint, it is much better if we use more descriptive names, for example:

  • deposit instead of d
  • rate instead of r
  • amount1 instead of a1

The longer and more descriptive names are good for a computer and also for a human being (that reads English).

# inputs
deposit = 1000
rate = 0.02

# amounts at the end of years 1, 2, and 3
amount1 = deposit * (1 + rate)
amount2 = amount1 * (1 + rate)
amount3 = amount2 * (1 + rate)
amount3
> [1] 1061.208

The idea of using descriptive names has to do with a broader topic known as literate programming. This term, coined by computer scientist Donald Knuth, involves the core idea of creating programs as being works of literature. As Donald puts it:

“Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.”

Donald Knuth (1984)

Whenever possible, make an effort to use descriptive names. While they don’t matter that much for the computer, they definitely can have a big impact on any person that takes a look at the code, which most of the times it’s going to be your future self. As it turns out, we tend to spend more time reviewing and reading code than writing it. So do yourself (and others) a favor by using descriptive names for your objects.

1.1.6 Combining various objects into a single one

We can store various computed values in a single object using the combine or catenate function c(). Simply list two or more objects inside this function, separating them by a comma ,. Here’s an example for how to use c() to define an object amounts containing the amounts at the end of years 1, 2, and 3.

# inputs
deposit = 1000
rate = 0.02

# amounts at the end of years 1, 2, and 3
amount1 = deposit * (1 + rate)
amount2 = amount1 * (1 + rate)
amount3 = amount2 * (1 + rate)

# combine (catenate) in a single object
amounts = c(amount1, amount2, amount3)
amounts
> [1] 1020.000 1040.400 1061.208

So far we have created a bunch of objects. You can use the list function ls() to display the names of the available objects. But what kind of objects are we dealing with?

It turns out that all the objects we have so far are vectors. You’ll learn about the basic properties of vectors in the next chapter.


1.2 Exercises

1) Area of a rectangle.

As you know, the area of a rectangle is the product of its length and width:

\[ \text{area of rectangle} = \text{length} \times \text{width} \]

Write R code to compute the area of a rectangle of a certain length and a certain width. You can give length and width numeric values of your preference. The computed area should be stored in an object area.


2) Area of a circle.

As you know, the area of a circle of radius \(r\) is given by

\[ \text{area of circle} = \pi \times r^2 \]

Write R code to compute the area of a circle of radius \(r = 5\). Write code in a way that you create a radius object, and an area object. By the way, R comes with a built-in constant pi for the number \(\pi\).


3) In this chapter we introduced the formula of Future Value. There is also the Present Value which is the current value of a future sum of money or stream of cash flows given a specified rate of return. Its equation is given by:

\[ \text{PV} = \text{FV} \times \frac{1}{(1 + r)^n} \]

where:

  • \(\text{FV}\) = Future Value
  • \(r\) = Rate of return
  • \(n\) = Number of periods

Write R code to compute the Present Value of $2,200 one year from now, knowing that the annual rate of return is \(r=3\%\). Try using descriptive names for the objects created to obtain the present value.


4) Consider the monthly bills of an undergraduate student:

  • cell phone $90
  • transportation $30
  • groceries $580
  • gym $15
  • rent $1700
  • other $85
  1. Use assignments to create variables phone, transportation, groceries, gym, rent, and other with their corresponding amounts.

  2. Create a total object with the sum of the expenses.

  3. Assuming that the student has the same expenses every month, how much would she spend during a school “term”? (assume the term involves five months).