#> [1] 6
1 Getting started
Getting Started with R
- When you type something into the console, R will give you a reply. For example:
- As you probably expected, R returned
6
.
- You can ignore the
[1]
you see in the returned value. That’s just R saying that6
is the 1st value (and only value here) of the output line.
- R can be used as a calculator.
(59 + 73 + 2) / 3
#> [1] 44.66667
10^(3+1)
#> [1] 10000
- Arithmetic operators are the same as they are in most other computer applications.
Arithmetic operators
Description | Operator |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponentiation | ^ or ** |
integer division 10%/%3 is 3 |
x %/% y |
modulus (x mod y) 10%%3 is 1 |
x %% y |
Exercise 1.1
Use R to calculate the following:
- Add 8 to 22 and then multiply the answer by 3
- Divide 8 by 2.5 and then divide the answer by 3
The above calculations do not produce any kind of output that is remembered by R
Creating objects
To store our calculations in R we need to give it a name and tell R to store that as an object.
Assignment operators
<-
or=
can be used to assign a value to an R object, and it is preferable to use<-
as=
has other uses.
Now that we’ve created this object, R knows all about it and will keep track of it during this current R session
All of the objects you create will be stored in the current workspace and you can view all the objects in your workspace in RStudio by clicking on the
Environment
tab in the top right-top pane
- If you click on the down arrow on the ‘List’ icon in the same pane and change to ‘Grid’ view, RStudio will show you a summary of the objects including
- the type (numeric - it’s a number)
- the length (only one value in this object)
- its ‘physical’ size, and
- its value (48 in this case)
- There are many different types of values that you can assign to an object. For example
- Here we have created an object called
my_obj2
and assigned it a value ofR is cool
which is a character string.
- We have enclosed the string in quotes.
- If you forget to use the quotes you will receive an error message.
#> Error in parse(text = input): <text>:1:14: unexpected symbol
#> 1: my_obj2 <- R is
#> ^
- Our workspace now contains both objects we’ve created so far with
my_obj2
listed as type character.
To change the value of an existing object we simply reassign a new value to it.
For example, to change the value of
my_obj2
from"R is cool"
to the number1024
Once we have created a few objects, we can do stuff with our objects.
For example, the following code creates a new object
my_obj3
and assigns it the value ofmy_obj
added tomy_obj2
which is
#> [1] 1072
#> Error in char_obj + char_obj2: non-numeric argument to binary operator
Reading the error message is important in R
This error message is essentially telling you that either one or both of the objects
char_obj
andchar_obj2
is not a number and therefore cannot be added together
- Another error message that you’ll get quite a lot when you first start using R is
Error: object '**' not found
. As an example, take a look at the code below.
#> Error: object 'no_obj' not found
R returns an error message because we haven’t created (defined) the object
no_obj
yet.If you check your environment, you’ll see that object
my_obj4
has not been created
Naming objects
-
In R, object names are case sensitive, and a valid object name (syntactic name) consists of a combination of
(
a-z
,A-Z
), (0-9
), (.
), (-
), and (_
)An object name cannot start with a number or a hyphen or an underscore and can start with a dot, but it must be followed by a letter
A good programming practice is to use meaningful object names in the codes, and self-explanatory object names increase the readability of the codes
Existing R functions, names, or words (e.g.,
mean
,log
,exp
,TRUE
,c
, etc.) should not be used as object names
Exercise 1.2
- Create an object called
x1
which is the number - Create another object called
x2
which is the answer to the sum - Multiply
x1
andx2
together and store the object as another object calledx3
- Subtract
fromx3
and calculate the 4th root. - The answer should be
Use of brackets
-
Parentheses
(
&)
are used to define arithmetic expressions, and they must be matched; unmatched parentheses will result in errors
((3 + 12)/3 + 8)
#> [1] 13
- Using curly brackets
{
&}
do not result in any error but should not be used as they have some specific uses in R, e.g. defining a function
{10 + 2}+ 5
#> [1] 17
- The square brackets
[
&]
cannot be used in arithmetic expressions
2 + 7]/3 [
#> Error in parse(text = input): <text>:1:1: unexpected '['
#> 1: [
#> ^
R functions
- Functions are ready-made pieces of codes.
- Some of the mathematical functions in R:
Description | R symbol | Example |
---|---|---|
square root | sqrt | sqrt(225) |
natural logarithm | log | log(50) |
exponential | exp | exp(3) |
absolute | abs | abs(-10) |
factorial | factorial | factorial(6) |
sine function | sin | sin(25) |
inverse cosine | acos | acos(-8.67) |
- R has a large collection of built-in functions that are called like this:
-
Let’s try using
sqrt()
-
sqrt(25)
orsqrt(x = 25)
will return a value5
-
- inputs (called arguments in R) should be within the parentheses
( )
even if there’s no input. - Multiple inputs, if needed, are separated by commas, e.g.
fun_name(
input1, input2, input3)
Some useful built-in R functions
#> [1] 3.57
#> [1] 3
#> [1] 4
#> [1] 3.141593
R ignores excess spacing
#> [1] 27
#> [1] 27
Basically, you can put spaces between different values, and you can put as many as you want and R won’t care.
However, if you insert a space within a single value, R will generate an error.
#> Error in parse(text = input): <text>:1:5: unexpected numeric constant
#> 1: 3 .14
#> ^
Commenting codes
R will ignore anything on the same line that follows the #
symbol.
#> [1] 24.22145
Exercise 1.3
- Why does this code not work?
#> Error: object 'my_varıable' not found
Exercise 1.4
Create an object called
myObject
and assign it a value between 1 and 100Add 13 to
myObject
, making sure the object itself stores the updated valueIs
myObject
divisible by 2? by 3? by 13? by 21? Use the R code to get the answer.How many times can 5 fit in
myObject
?