# 4 Objects in R

R is what is known as an *“object-oriented”* language. For example, you can give a name to a mathematical expression, which has the benefit (and effect) of storing it for later use. Indeed, R does not usually display the results of analyses you perform right away like in SPSS, SAS or Stata. Instead, performed analyses are usually stored in an object for later use. For this reason, when doing statistics in R, you will often find yourself naming and storing objects. The name you choose should consist of letters, numbers, and the “.”, and it should not start with a number.

In order to create an object in R, you need to assign it to a name. Names can be assigned by using the arrow-like signs <- and -> as demonstrated in the exercise below. Which sign you use depends on whether you prefer putting the name first or last (it may be helpful to think of -> as “put into” and <- as “set to”). It is also very common to use the equal sign ‘=’. If you who would like to learn more about the differences between the assign signs, here’s a good link on the matter.

In order to see the result, you run the name of the object

`[1] 103`

Store the number 0.0001 under the name “small.num” (i.e. put 0.0001 into small.num). Note that R does not produce any output.

Now retrieve its value by running the saved object

`[1] 0.001`

You can put the name first if you reverse the arrow (set big.num to 10 * 100). This is easier.

`[1] 1000`

A third way this is done is by using the equal sign like this.

Every object in R has a type - all the variables we created are termed scalar. Scalar variable is a single number variable. x is a scalar variable whose value is 103, small.num is a scalar variable whose value is 0.001 and so forth.

## 4.1 Practicals

In this lesson we will practice what we learned so far.

These are a few exercises that should help you get acquainted with R. Try to calculate or evaluate the R expressions before pressing the “R Output” button.

R commands can sometimes be rather difficult to follow, so occasionally it can be useful to annotate them with comments. This can be done by typing a hash (#) character: any further text on the same line is ignored by R. Copy the code below onto your Source pane (top-left) and run it while reading the comments.

`[1] 2`

`[1] 500`

`[1] 5`

`[1] 104`

`[1] 4.5`

`[1] 3`

`[1] 4.60517`

`[1] 1`

`[1] 2`

`[1] 2`

`[1] Inf`

`[1] -Inf`

`[1] NaN`

`[1] 0+3i`

`[1] 7`

`[1] 3`