Introduction To R and Rstudio, R Script, Calling Functions, Running Code
Introduction To R and Rstudio, R Script, Calling Functions, Running Code
Introduction To R and Rstudio, R Script, Calling Functions, Running Code
running code.
Dr Diana Abdul Wahab
Sem II 2021/2022
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.
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.
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)
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.
Matrices
matrix(1:12, nrow=3, ncol=4)
matrix(1:12,nrow=3)
Matrices
matrix(1:3,nrow=3,ncol=4)
Lists
These are a bit like vectors, except that each entry can be any other R object, even another
list.
x <- list(1:3, TRUE, "Hello", list(1:2, 5))
x
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "Hello"
##
## [[4]]
## [[4]][[1]]
## [1] 1 2
##
## [[4]][[2]]
## [1] 5
Looping
The most common way to execute a block of code multiple times is with a for() loop. What’s
going on in the code below?
factorial2 = function(n) {
out = 1
for (i in 1:n) {
out = out*i
}
out
}
factorial2(10)
## [1] 3628800
Conditional Code
abs2 = function(x) {
if (x < 0) out = -x
else out = x
out
}
abs2(-4)
## [1] 4
No try lm!
There are two important parts to the function: the signature, which in this case is
function(x, y), and the body, which is the code between the curly brackets. Broadly
speaking, when a function is called, it takes the information in the arguments, applies the
code in the body to them, and then spits out the final expression in the function.
var(1:10)
## [1] 9.166667
Square function:
square = function(x) {
x^2
}
square(4)
## [1] 16
## [1] 5.5
Exercise
Can you create a function to compute standard deviation?
Compute the standard deviation of this vector: 1 6 3 4 2 7
(Hint: sd = sqrt(sum(x-mean)/(n-1)) )
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")