SC Lecture01 PDF
SC Lecture01 PDF
SC Lecture01 PDF
script.
Dr Diana Abdul Wahab
Sem II 2022/2023
Introduction to R
What we’re going to do today:
1. Obtaining and installing R and provide an overview of its uses and general
information on getting started.
2. Using the text editors for the code and provide recommendations for the general
working style.
3. Obtaining assistance using help files.
4. Loading packages.
## [1] 3
x <- 10
x - 2
## [1] 8
Note: Assignment can also be done with = (or <-). But in this class I’d prefer to use the <-
sign.
Vector
The key feature which makes R very useful for statistics is that it is vectorized. This means
that many operations can be performed point-wise on a vector. The function c() is used to
create vectors:
x <- c(1,-1,3.5,2)
x
x+2
sum((x - mean(x))^2)
## [1] 10.6875
Exercise
1. What is a vector?
2. The weights of five people before and after a diet program are given:
Before: 78 72 78 79 105
After: 67 65 79 70 93
Read the ‘before’ and ‘after’ values into two different vectors called before and after. Use R
to evaluate the amount of weight lost for each participant. What is the average amount of
weight lost?
Colon Operator
Some useful vectors can be created quickly with R. The colon operator is used to generate
integer sequences
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
9:5
## [1] 9 8 7 6 5
2^(0:10)
## [1] 1 2 3 11 12 13 21 22 23 31 32 33
seq() Function
More generally, the function seq() can generate any arithmetic progression.
seq(from=2, to=6, by=0.4)
## [1] 2.0 2.4 2.8 3.2 3.6 4.0 4.4 4.8 5.2 5.6 6.0
rep() Function
Sometimes it’s necessary to have repeated values, for which we use rep()
rep(5,3)
## [1] 5 5 5
rep(2:5,each=3)
## [1] 2 2 2 3 3 3 4 4 4 5 5 5
rep(-1:3, length.out=10)
## [1] -1 0 1 2 3 -1 0 1 2 3
Exercise
Create the following vectors in R using seq() and rep().
• 1, 1.5, 2, 2.5, … , 12
• 1, 8, 27, 64 ,…, 1000.
• 1, 0, 3, 0, 5, 0, 7 ,…, 0, 49.
Subsetting
x <- c(5,9,2,14,-4)
x[3]
## [1] 2
x[c(2,3,5)]
## [1] 9 2 -4
x[1:3]
## [1] 5 9 2
x[3:length(x)]
## [1] 2 14 -4
x > 4
x[x > 4]
## [1] 5 9 14
x[-1]
## [1] 9 2 14 -4
x[-c(1,4)]
## [1] 9 2 -4
Exercise
The built-in vector LETTERS contains the uppercase letters of the alphabet. Produce a
vector of - the first 12 letters - the odd ‘numbered’ letters - the (English) consonants.
Logical Operators
x <= 2
x == 2
x != 2
(x==5)|(x>10)
!(x>5)
Exercise
The function rnorm() generates normal random variables. For instance, rnorm(10) gives a
vector of 10 i.i.d. standard normals. Generate 20 standard normals, and store them as x.
Then obtain subvectors of
• the entries in x which are less than 1;
• the entries between -1/2 and 1;
• the entries whose absolute value is larger than 1.5.
Packages
R comes with a series of default packages. A package is a collection of previously
programmed functions, often including functions for specific tasks. It is tempting to call this
a library, but the R community refers to it as a package.
There are two types of packages: those that come with the base installation of R and
packages that you must manually download and install. With the base installation we mean
the big executable file that you downloaded and installed before. The base version contains
the most common packages.
There are literally hundreds of user-contributed packages that are not part of the base
installation, most of which are available on the R website. Many packages are available that
will allow you to execute the same statistical calculations as commercial packages. For
example, the multivariate vegan package can execute methods that are possible using
commercial packages such as PRIMER, PCORD, CANOCO, and Brodgar.
Loading a package that came with the base installation may be accomplished either by a
mouse click or by entering a specific command. For instance, to load the MASS package,
type the command:
library(MASS)
and press enter. You now have access to all functions in the MASS package. So what next?
You could read a book, such as that by Venables and Ripley (2002), to learn all that you can
do with this package.
If you need to use a package not included in the base installation, you can type:
install.packages("name_of_package")
library("name_of_package")