Executing commands in R
Clicking on the R icon, or opening RStudio similarly, should provide you
with access to a window or pane, called the R console in which you can
execute commands.*
The > sign is the R
prompt which indicates
where you can type in
the command to be ex-
ecuted.
*A short video can be found on Canvas to show how to start running R.
2
Executing commands in R
You can do arithmetic of any type, including multiplication:
By hitting the “Enter”
key, you are asking R to
execute this
calculation.
3
What else to expect in this lecture
1. Running (executing) commands in R
2. Doing arithmetic calculations and storing the results for future use.
3. Quitting the R session.
4
R can be used as a calculator
GUESS what do we type in R for calculation below:
√
• 745 − 238; 745 + 238; 745 × 238; 745 ÷ 238; 73; 38;
√
• log 4; 16; ex; 5!; sin 2; cos 2; tan 2; sin 2;
√
• log(745 − 238 × 2) + 38
5
R can be used as a calculator
*
##### the first group
745-238
## [1] 507
745+238
## [1] 983
745*238
## [1] 177310
745/238
* Note that R ignores everything typed to the right of #.
6
## [1] 3.130252
7ˆ3
## [1] 343
38ˆ(1/2)
## [1] 6.164414
##### the second group
log(4)
## [1] 1.386294
sqrt(16)
## [1] 4
exp(3)
## [1] 20.08554
factorial(5)
## [1] 120
sin(2)
## [1] 0.9092974
cos(2)
## [1] -0.4161468
tan(2)
## [1] -2.18504
sin(2)
## [1] 0.9092974
#### the third group
log(745-238*2) + sqrt(38)
## [1] 11.75913
Executing commands in R
Objects that are built in to R or saved in your workspace, i.e. the
environment in which you are currently doing your calculations, can be
displayed, simply by invoking their name.
> women
For example, the data ## height weight
set or data frame called ## 1 58 115
## 2 59 117
women contains infor-
## 3 60 120
mation on heights and ## 4 61 123
weights of American ## 5 62 126
## 6 63 129
women: ## 7 64 132
## 8 65 135
## 9 66 139
## 10 67 142
## 11 68 146
## 12 69 150
## 13 70 154
## 14 71 159
## 15 72 164
7
Some Result from Calculation
If I want to subtract 238 divide 0 in R, do I just type
(745-238) / 0
## [1] Inf
If I want to subtract 238 divide 0 in R but accidently type o instead 0, the
result is
(745-238) / o
## Error in eval(expr, envir, enclos): object ’o’ not
found
8
Stored Number
The area A of a circle of radius r can be found from the formula
A = πr2
and R has stored a value of π as
pi
## [1] 3.141593
Now calculate the area of a circle with radius 1
pi*1ˆ2
9
Result
pi*1ˆ2
## [1] 3.141593
10
Calculations in R
You can control the number of digits in the output with the options()
function.
This is useful when reporting final results such as means and standard
deviations, since including excessive numbers of digits can give a
misleading impression of the accuracy in your results.
options(digits=3)
583/31
583/31
Compare with ## [1] 18.8
## [1] 18.80645
pi
## [1] 3.14
11
Calculations in R
Observe the patterns in the following calculations.
options(digits = 18)
1111111*1111111 The error in the final calculation is
## [1] 1234567654321 due to the way R stores information
11111111*11111111 about numbers.
## [1] 123456787654321 There are around 17 digits of
111111111*111111111 numeric storage available.
## [1] 12345678987654320
12
More Calculations with R
We can compute the remainder after division of 17 by 6
17 %% 6
## [1] 5
This is called modular arithmetic.
The calculation above is referred to as “17 (mod 6).”
13
More Calculations with R
We can also calculate the quotient, without remainder, using %/%:
17 %/% 6
## [1] 2
2 * 6 + 5 # check the calculation
## [1] 17
14
What are the numbers in square brackets?
The following example displays the data in rivers, lengths of 141 North
American rivers (in miles). The second line starts with the 12th value,
and the third line stars with the 23rd value, and so on.*
options(width=60)
rivers
## [1] 735 320 325 392 524 450 1459 135 465 600 330
## [12] 336 280 315 870 906 202 329 290 1000 600 505
## [23] 1450 840 1243 890 350 407 286 280 525 720 390
## [34] 250 327 230 265 850 210 630 260 230 360 730
## [45] 600 306 390 420 291 710 340 217 281 352 259
## [56] 250 470 680 570 350 300 560 900 625 332 2348
## [67] 1171 3710 2315 2533 780 280 410 460 260 255 431
## [78] 350 760 618 338 981 1306 500 696 605 250 411
## [89] 1054 735 233 435 490 310 460 383 375 1270 545
## [100] 445 1885 380 300 380 377 425 276 210 800 420
## [111] 350 360 538 1100 1205 314 237 610 360 540 1038
## [122] 424 310 300 444 301 268 620 215 652 900 525
## [133] 246 360 529 500 720 270 430 671 1770
* The line break is based on the optional setting options(width=60).
15
Simple Number Patterns
The : operator yields increasing/decresing sequences of numbers. For
example,
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
10:1
## [1] 10 9 8 7 6 5 4 3 2 1
Here is what happens when you add 3 to the above sequence:
(1:10)+3
## [1] 4 5 6 7 8 9 10 11 12 13
16
Simple Number Patterns
You can subtract or multiply a number too:
(1:10)-3
## [1] -2 -1 0 1 2 3 4 5 6 7
(1:10)*3
## [1] 3 6 9 12 15 18 21 24 27 30
17
More patterns
You can take powers of all elements in your sequence:
(1:10)ˆ2
## [1] 1 4 9 16 25 36 49 64 81 100
(1:10)ˆ3
## [1] 1 8 27 64 125 216 343 512 729 1000
18
Mannual Calculation
We can divide the numbers from 1 through 10 by 3, ignoring the
remainder using
(1:10)%/%3
What is the output in R?
19
Polling question - Answer
• 1 2 3 4 5 6 7 8 9 10
• Dividing 1 by 3 gives 0, and so does dividing 2 by 3.
• Dividing 3 by 3 gives 1, and so does dividing 4 and 5 by 3, and so on.
• Answer: 0 0 1 1 1 2 2 2 3 3, and check with R
(1:10)%/%3
## [1] 0 0 1 1 1 2 2 2 3 3
20
Examples
To get the sequence:
• 4, 5, 6, 7
• −100, −99, −98, −97, −96
• 214, 215, 216, 217, 218, 219
What do we do?
21
Examples
4:7
## [1] 4 5 6 7
-100:-96
## [1] -100 -99 -98 -97 -96
214:219
## [1] 214 215 216 217 218 219
63:59
## [1] 63 62 61 60 59
Named storage
When you begin an R session, you open a workspace known as the
global environment.
This environment is where you can begin to store the results of your
work.
For example, you might want to keep track of some calculations, or you
might have invented a new function to solve some kind of problem.
To store your output, you need to provide names for each object that you
want to save.
22
Named storage: Example
The rivers object contains measurements in miles. To convert to
kilometers, we can divide all of the measurements by 1.609:
riversKm <- rivers *1.609
The above command causes R to assign the calculated values to
riversKm using an arrow that points to the left, created with the
less-than sign (<) and the hyphen (-).
Note that no output appears. You can see the results of this assignment
by typing
riversKm
23
Converted to feet
There are 5280 feet in one mile. That means we can convert the lengths
of the rivers from miles to feet by multiplyng each value by 5280. Does
the following code assign these lengths to the object riversFeet?
riversFeet < rivers*5280
24
Converted to Feet
No! You need <-, not just <
riversFeet <- rivers*5280
25
Quitting R
To quit your R session, type q()
q()
If you then hit the Enter key, you will be asked whether to save an image
of the current workspace, or not, or to cancel.
The workspace image contains a record of the computations you’ve
done, and may contain some saved results.
26
Functions
Most of the work in R is done using functions.
For example, we saw that to quit R we type q(). This tells R to call the
function named q.
The brackets surround the argument list, which in this case contains
nothing: we just want R to quit, and do not need to tell it how.
27
q is a function
Attempting to quit without the brackets gives:
q
## function (save = "default", status = 0, runLast = TRUE)
## .Internal(quit(save, status, runLast))
## <bytecode: 0x562ab2c48210>
## <environment: namespace:base>
This has happened because q is a function that is used to
tell R to quit.
28
q is a function
Typing q by itself tells R to show us the contents of the function q. By
typing q(), we are telling R to call the function q.
The action of this function is to quit R.
q has three arguments: save, status, and runLast.
29
Default Values of Parameters
Each argument has a default value: "default", 0, and TRUE
respectively.
What happens when we execute q() is that R calls the q function with
the arguments set to their default values.
30
Changing from the Defaults
To change from the default values, specify them in the function call.
Arguments are identified by their position or by their name.
For example,
q("no") # these calls tell R to quit without
q(save = "no") # saving the workspace image
31
Changing from the Defaults
If we had given two arguments without names, they would apply to save
and status.
If we want to accept the defaults of the early parameters but change later
ones, we give the name when calling the function, e.g.
q(runLast = FALSE)
32
Changing from the Defaults
Alternatively, commas can be used to mark the missing arguments, e.g.
q( , , FALSE)
It is a good idea to use named arguments when calling a function which
has many arguments or when using uncommon arguments, because it
reduces the risk of specifying the wrong argument, and makes your
code easier to read.
33