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.
You can store the results of mathematical expressions:
x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100
Here, you are storing the result of this mathematical expression sqrt(2 * max(-10, 0.2, 4.5)) + 100
into an object you called x
.
The function max() picks the the larger number within the function’s argument, then multiplies by 2, and takes the squared root. Then it saves to an object called x. If you want to learn more about the function max() type ?max
into the console. Works for every function in R!
Let’s try now. In order to see the result, type the code, then run the name of the object
## [1] 13
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.
```
# this is a comment:
# R will ignore it
1 + 1 # this equals to two
# You can make short comments as above
# Or long as below:
# The symbol * means multiply, and ^ means "to the power", so this gives 5 times (10 squared), i.e. 500
5*10^2
# A different function: "sqrt" takes a
# single argument, returning its square root.
sqrt(25)
# The result of a function can be used
# as part of a further analysis
sqrt(25-9)+100
# For example, let's learn about the
# function max(). This function returns
# the maximum value of all its arguments
max(-10, 0.2, 4.5)
# Now you can use results of functions
# as arguments to other functions
sqrt(2 * max(-10, 0.2, 4.5))
# The log() function returns the
# logarithm of its first argument
log(100)
# By default this is the natural logarithm (base "e")
log(2.718282)
# But you can change the base of the
# logarithm using the "base" argument
log(100, base=10)
log10(100)
# R knows about infinity (and minus infinity)
1/0
-1/0
# undefined results take the value NaN ("not a number")
0/0
# for the mathematically inclined,
# you can force R to use complex numbers
sqrt(0i-9)
# R has another special symbol for 'empty'
sum(c(1, 2, NULL, 4))
length(c(1, 2, NULL, 4))
```