Working with R at the command line

Let’s do some more with the command line. First, well start off with a simple expression:

8 + 2

Once you’ve entered this (by pressing Enter or Return), R should immediately return the following:

[1] 10

Leaving aside the [1] for the moment, you can see from this interaction R can identify these as numbers, as well as the operator + for addition, so it will perform the addition operation on the two numbers. R uses a number of standard operators for basic math operations, such as:

Some math operators in R
Operation Example
Addition 8 + 2
Subtraction 8 - 2
Multiplication 8 * 2
Division 8 / 2
Exponent 8 ^ 2

But these sorts of things are about the limits of what R knows by itself. For example, we could throw R for a loop by asking it to add the letter a to the number 2:

a + 2

This should produce an error that looks something like this:

Error: object 'a' not found

You and I know that you can’t just add the letter a to the number 2. But R doesnt know why this is the case. R doesnt even know what the letter a is. All R knows is that we’ve asked it to add 2 to something unfamiliar labeled a.

Assigning values to objects

Imagine for a moment that this were an algebra problem, and a stood for something that can be added to 2. In algebra, this is known as a variable. In order for the computer to know what the variable a is, we have to define it as an object in the computer’s memory. To define something in R, we use the assignment operator, which is composed of the ‘less than’ symbol and a hyphen, which taken together resemble an arrow like so:

<-

For example, if we wanted to define a as having a value of 8, we can do it this way:

a<-8

R will read this command as “Let a have a value of 8”. Now if we ask the computer to tell us what a is:

a

It should respond like this:

[1] 8

R now interprets the letter a as being equivalent to the number 8. If we go back to our original expression of a + 2

a + 2

we will get the same answer as 8 + 2:

[1] 10

Again, don’t worry too much about the number in square brackets, we’ll get to that soon.

Try it yourself!
  • Try adding, subtracting, multiplying a by other values. What about stringing together multiple math operators?

  • The assignment operator (<-) can work in the other direction (->) as well! Try assigning 6 to the variable b with ->, keeping in mind what sides 6 and b should be on.

  • What do you think will happen if you multiply a by b?

Updating a value

At the moment, R recognizes the letter a as standing for the number 8. We can change this value simply by reassigning a new value to a:

a<-9

Now a is 9. And when we add 2…

a + 2

…we get a new value:

[1] 11

Great! Because R currently reads a as a number (9), we can also assign a new value to the object a using it inside an expression, like this:

a<-a + 1

R interprets this as something like “Let a have a value of a plus 1”. Since a was equal to 9, this new assignment gives it that value plus 1. So if we look at the value of a

a

…we should see it updated from 9 to 10:

[1] 10

By using the assignment operator before the expression, our value for a is now the result of that expression: 10. At the moment, this may not seem very impressive, updating values of stored objects is one of the keys of computer programming.

Try it yourself!

Try guessing what the final value of a would be if you entered the following commands in order:

a+8

a-5

a<-a+2

a<-16

a<-a/2

Note: sorry if these are purple instead of blue, you can still enter them at the command line!

Functions

We can also perform a number of built-in functions on objects like numbers or variables. Functions operate typically by stating the function name in R, followed by a set of input values, called arguments, that are contained in parentheses (). For example, let’s set to a value of 5:

a<-5

Now if we wanted to find the square root of the current value of a, we could use the sqrt function like so:

sqrt(a)

…we should get the following:

[1] 2.236068

Alternatively, a situation might come up where you want to repeat the value of a 10 times. To do this, you can use the rep function:

rep(a,10)

This gives us the following:

[1] 5 5 5 5 5 5 5 5 5 5

The rep function accepts two arguments: the object to be repeated (a), and the number of times it is to be repeated (10). These two items are separated by a comma. The function produces ten instances of the value of a, which is ten 5s. This type of data is called a vector, or a collection of values of a given type (in this case, numbers). Different functions will accept different arguments and produce different types of objects, and this information is given in the Help documentation.

So what’s the deal with [1] appearing next to each answer so far? The [1] indicates the position of the value immediately following it in a vector. This is particularly useful when you’re looking at very long vectors. For example, try using rep to create 100 instances of a:

rep(a,100)

The result should look something like this (your position numbers may be slightly different):

[1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

[37] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

[73] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Here, the first line starts contains elements 1 to 36 of the vector. The second line starts at element 37, indicated by the number in square brackets, and contains elements 37 to 72. The last line contains elements 73 to 100.

Most of R’s functions are vectorized, which means that they work simultaneously across all elements in a vector. We’ll learn more about this later on, but just keep in mind that these numbers in square brackets are just marking position in a vector.