In this handout, I’m going to cover the absolute basics of operations with R. We’ll cover addition, subtraction, multiplication, division, and elementary logical operations.
Let’s go ahead and start with some math. Type the following commands into R and hit ``enter" after each line:
1 + 1
1 - 1
1 * 1
1/1
They should be easy: + means add and - means subtract. Using * for multiply makes more sense if you see if as the times operator, not an x sign: for reasons that should be obvious, x x y wouldn’t be easy to use, while x * y is. Finally, the / operator for division is also on the more intuitive side.
These examples show the simplest operators we’ll use.
Write out what the R code would be to
Other R operators are a little less familiar for most people. For instance, to exponentiate a number, we use the carat (^), which is shift-6 on most keyboards. So, to take the square of 4, we would write
4^2
or to raise 3 to the third power, we would write:
3^3
Go ahead and type these into R to see what they produce.
One of the great advantages of R is that it also lets us define our own variables, just like algebra. So we can define any letter (or, later, words) to stand for any data type, including numbers. Let’s see how that works:
a <- 4 #The <- is called the `assignment operator'; Bailey uses `=` for this, but I prefer `<-`
a^2
b <- 3
b * a
b / a
a + b
Write r code that
A great strength of learning how to program is the ability to carry out simple logical comparisons. R treats six data types as fundamental. So far we’ve seen one of them: numeric. Now let’s meet another one: logical.
A logical data type may only either be TRUE or FALSE. We use a standard set of logical operators to test logical relationships; a full list can be found here and elsewhere on the internet. Let’s see how these work:
2 > 3
2 < 3
2 != 3
These are nice, but we’re often interested in more complicated relationships. For instance,
a <- 3
b <- 4
c <- 5
c^2 == b^2 + a^2 #note the double equals sign!
c^3 >= b^2 + a^2
c^2 >= b^2 + a^2
c^2 <= b^2 + a^2
c^2 < b^2 + a^2
Note that R will evaluate logical statements, not judge us on whether they’re correct as entered or not. If you want to ask R if 5 is less than 4, or if blue is the same as red, R won’t break: those are valid questions, and R will simply check whether they are true or not.
Write r code that
These are pretty basic operations. We want to put them to better use. Part of that is beginning to move beyond the idea of adding individual numebrs and instead start thinking about operations involving strings of numbers–mathematical matrices. For instance:
g <- c(1,2,3,4) # c() is the combine function; it puts together elements as a vector.
g + 2
g * 2
This is the most basic kind of matrix algebra. You don’t need to relearn all of matrix algebra, although brushing up on it is useful. But you should have the intuition that we can perform more complicated operations using R. We can even multiply one matrix by another:
h <- c(1,2,3,4)
g * h
g %*% h
You normally won’t need to do these sorts of things. I’m showing them to you so you begin to develop an intuition for what the computer is doing ``under the hood." One of the most useful implications of this, by the way, is that we can also divide:
g/h
How can this be useful? Well, let’s enter in some data by hand. First, we’re going to type in (by hand) Saudi Arabia’s production of petroleum for the years 1980-1990 (from the EIA Web site):
saudi.production <- c(10285,10265,6929,5430,5033,3778,5255,4674,5577,5555,7019)
Now let’s enter the corresponding US amounts:
usa.production <- c(10809,10739,10783,10788,11107,11192,10905,10648,10473,9880,9678)
We can use the length() function to ask how long these vectors are:
length(saudi.production)
length(usa.production)
length(usa.production) == length(saudi.production)
We can also derive some basic summary statistics:
mean(saudi.production)
median(saudi.production)
range(saudi.production)
And we can also find out the ratio of Saudi to US production in any given year:
ratio <- saudi.production/usa.production
ratio #If you type an R object's name and hit enter, it will display that object's value
## [1] 0.9515219 0.9558618 0.6425856 0.5033370 0.4531377 0.3375625 0.4818890
## [8] 0.4389557 0.5325122 0.5622470 0.7252532
What does this math tell us?
Write r code that