Computing-II - Lecture Notes-I
Computing-II - Lecture Notes-I
Ambo University
Using
R & SAS
By: Senahara Korsa (PhD)
3/29/2023
Introduction to R
To support your learning and understanding of R, the first thing
you will need to do is download and install both R and RStudio on
your computer.
Take a look at the Setup link
https://alexd106.github.io/intro2R/setup.html for further
instructions on how to set up your computer and download the
required datasets.
To get up and running the first thing you need to do is install R.
R is freely available for Windows from the Comprehensive R
Archive Network (CRAN) website.
Click on the link below for step-by-step instructions on how to
download and install R and Rstudio
https://alexd106.github.io/intro2R/howto.html#install
Senahara Korsa (PhD) 3/29/2023 2
What is R?
• R is a free language and environment for statistical computing and
graphics.
• R was initially written in the 1990s by Ross Ihaka and Robert
Gentleman.
• The name is partly based on the (first) names of the first two R
authors (Robert Gentleman and Ross Ihaka).
• R is widely used programming language for statistical analysis,
predictive modeling and data science
Some tasks of R are:
• Exploring and Manipulating Data
• Building and validating predictive models
• Applying machine learning and text mining algorithms
• Creating visual appealing graphs
• Connecting with Databases
Senahara Korsa (PhD) 3/29/2023 3
Download R and Rstudio
Download R from:
http://cran.r-project.org/bin/
• R and RStudio are not two different versions of the same thing.
• In fact, they work together.
• R is a programming language for statistical calculation.
• And RStudio is an Integrated Development Environment (IDE)
that helps you develop programs in R.
• You can use R without using RStudio, but you cannot use
RStudio without using R, so R comes first.
• Rstudio is not R or a type of R, rather it is a program that runs R
and provides extra tools that are helpful when writing R code.
• Everything that is conducted in Rstudio can also be done
directly on R.
1. R is a case-sensitive package.
• For instance: AGE, Age, and age are different objects!
2. R as calculator
• You can use R as a powerful calculator for a wide range of
numerical computations.
Example:
> log2(32)
[1] 5
>seq(0, 5, length=6)
[1] 0 1 2 3 4 5
>plot(sin(seq(0, 2*pi, length=100)))
The [1] tells you the resulting value is the first result.
• Finding Variables
• To know all the variables currently available in the workspace
we use the ls() function.
• Deleting Variables
• Variables can be deleted by using the rm() function.
• To remove object x, for instance, use:
>rm(x) #to delete specific object x
>rm(list = ls())# To remove all currently defined objects
> save(file="file-name.RData") or
> save.image("filen-name.RData") or
> sink()
> example("topic")
>example(lm)
> ? help
> objects()
> ls()
Numeric vectors
Character vectors
Logical vectors
> y<-c(x,0,x)
> y<-c(x,0,x)
> y
[1] 10 6 3 6 22 0 10 6 3 6 22
Note: The [1] in front of the result is the index of the first
row in the vector x.
> sin(x)
> cos(x)
> exp(x)
Example
Example:
The logical operators are <, <=, >, >=, = = for exact
equality and != for inequality.
In addition if c1 and c2 are logical expressions, then c1&c2 is
their intersection(“and”), c1|c2 is their union(“or”), and !c1 is
the negation of c1.
Senahara Korsa (PhD) 3/29/2023 44
Missing Values inVectors
The function is.na(x) gives a logical vector of the same size as x
with value TRUE if and only if the corresponding element in x is
NA = not available or a missing value. Example:
Example:
> const>2
pi euler sqrt2 golden
TRUE TRUE FALSE FALSE
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
Senahara Korsa (PhD) 3/29/2023 49
Generating sequences…
The colon operator has high priority within an expression.
> 2*1:10
[1] 2 4 6 8 10 12 14 16 18 20
> 2*(1:10)
[1] 2 4 6 8 10 12 14 16 18 20
The other option to generate numbers is using seq() function
Example:
> seq(1:10)
Example:
>seq(1,5, by=2)
[1] 1 3 5
> seq(1,10, length=5)
will print each element of x five times before moving onto the next.
Further more, the command
> rep(c(1,4), c(2,3))
Example
> x
[1,] 1 2 3 4
[2,] 5 6 7 8
An equivalent expression:
> x<-matrix(c(1:8),nrow=2,ncol=4)
Example:
> cbind(c(1,2,3),c(4,5,6))
> rbind(c(1,2,3),c(4,5,6))
Name Operation
dim() Dimension of the matrix (number of rows and columns)
as.matrix() Used to convert an argument into a matrix object
%*% Matrix multiplication
t() Matrix transpose
det() Determinant of a square matrix
solve() Matrix inverse; also solves a system of linear equations
eigen() Computes eigenvalues and eigenvectors
That is, the elements of a list can be objects of any type and
structure.
L<-list(object-1,object-2,…,object-m)
[1] 1 5 3
[1] 2
>L[[c(3,2)]] # Recursively: element 3 of L, hereof the 2nd element [1] "world”
> #OR
> L[[3]][2]
[1] "world"
Data frames can have columns of different data types and are
the most convenient data structure for data analysis in R.
Data frames are lists with the constraint that all elements are
vectors of the same length.
>dat<-data.frame(object-1,object-2,…,object-m)