Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/334274516

How to use the R software

Poster · July 2019


DOI: 10.13140/RG.2.2.18152.83206

CITATIONS READS

0 14,702

1 author:

Sami Mestiri
Faculté des Sciences Économiques et de Gestion de Mahdia
30 PUBLICATIONS   8 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Credit Risk Prediction based on Bayesian estimation of logistic regression model with random effects View project

Semiparametric count model for credit risk system View project

All content following this page was uploaded by Sami Mestiri on 29 January 2020.

The user has requested enhancement of the downloaded file.


How to use the R software

Sami Mestiri

EAS-Mahdia Research Unit


Faculty of Science economic and management of Mahdia,
Email : mestirisami2007 ∂ gmail.com

29 janvier 2020

Sami Mestiri How to use the R software


Sami Mestiri How to use the R software
Overview

J R is a language and software that allows statistical


analysis.
J It includes means that make it possible to
manipulate data, calculations and graphical
representations.
J It also has the possibility to run programs stored in
text files.

Sami Mestiri How to use the R software


Overview

Indeed R owns :
J An efficient system for handling and storing data.
J Different operators for calculating on arrays,
especially matrix.
J A lot of tools for data analysis and statistical
methods.
J Graphical means to visualize the analyzes.
J A simple and powerful programming language.

Sami Mestiri How to use the R software


Usefulness

J Designers and developers consider R as an


environment in which statistical techniques are run.
J R allows to study certain statistical procedures.
- The generalized linear models.
-The time series.
-Parametric and non-parametric tests, ...
J R provides a flexible graphical environment to
visualize and create various kinds of data presentation

Sami Mestiri How to use the R software


Usefulness

J R can extend its functions by through Packages.


There are currently hundreds of Packages are
available through the CRAN.
J Packages are available for specific purposes and
present a wide range of modern statistics. (Analysis
Descriptive Multidimensional Data, Regression Trees
and classification, three-dimensional graphics, etc ...)

Sami Mestiri How to use the R software


Installation

J R is free software that is distributed under the


terms of the GNU Public License.
J R has an official website at http :
www.R-project.org /
The procedure to follow is :
J Go to the page corresponding to the link
Download, CRAN.
J Download the installation file.
J Run the SetupR.exe program.
J Follow the instructions on the screen.
Sami Mestiri How to use the R software
The working space

J To launch R, you must click on the shortcut icon


R.
J When the R software is launched, it appears a new
interface (Rgui) with an inner window (R console).
J R is an online ordering software, that is to say that
the instructions are given one after the other.
J The console displays the results.

Sami Mestiri How to use the R software


The working space

J The on line commands are written on "R script"


then they are executed on a workspace "R console".
J R is an object-oriented language such that
instructions can be stored in objects that can be used
in turn.
J To be able to give an instruction, the user must
"have that the software must be ready to receive it.
the case when the prompt ("<") is visible

Sami Mestiri How to use the R software


Simple manipulations

J R can be used to perform simple calculations. If


we type :
5+4
R returns :
9
J This answer means that the result is a vector
whose first coordinate is ‘9 ’.
(R does not know the scalars, he considers these as
vectors of length 1).

Sami Mestiri How to use the R software


Simple manipulations

J The result ’9’ is just returned to the screen, it is


not stored in memory.
J You can create or overwrite objects with the
assignment operator. x = 5 + 4
J The content of an object is visible by typing its
name :
x
J R do difference between upper and lower case. (So
‘A ’and‘ a’ represent two different objects)

Sami Mestiri How to use the R software


Simple manipulations

J Separate the different commands with semicolons ;


or by line breaks. a = 3 ; b = 2
J To add comments, use the pound symbol #.
a # Gives the value of a
J When a command is not complete at the end of a
line, R returns a different prompt ’+’ until the
command is completed.
3-
+2
1
Sami Mestiri How to use the R software
Data with R

J We call objects, the entities created and


manipulated by R.
J Objects can be vector, factor, array, matrix,
data.frame, or a list.
J To store an object in memory, you must first
create it (often using a function), then tell R to store
it by assigning a name.
J Object Name = Object.
Example : a = 2

Sami Mestiri How to use the R software


Creation of a vector

J Vectors are the most basic data types in R. The


simplest forms are : numeric, character and logical
(TRUE or FALSE) :
J To create a vector we can use the function c ().
x = c(10,32,25,50,43)
x
[1] 10 32 25 50 43

Sami Mestiri How to use the R software


Creation of a vector

J seq() (sequence) generate a series of equidistant


numbers.
seq(from=n, to=m, by=t).
s= seq(1,10,0.8)
s
1.0 1.8 2.6 3.4 4.2 5.0 5.8 6.6 7.4 8.2 9.0 9.8
Jc () : concatenation of one or more vectors :
c(1, 5, 6, 9)
1 5 6 9
c(1 :5, seq(10, 20, 2))

Sami Mestiri How to use the R software


Manipulation of a vector

x= c(10,32,25,50,43)
x
sort(x) # classify the elements of a vector
[1] 10 25 32 43 50
order(x) # give indices vector elements
[1] 1 3 2 5 4
sum(x) # sum the elements of the vector
[1] 160
length(x) # give the length of a vector
[1] 5

Sami Mestiri How to use the R software


Manipulation of a vector

J A feature of R is that the elements of a vector can


have names.
J The function names () makes it possible to
associate a label with each element of a vector :
names(x) = c("a","b","c","d","e")
x
a b c d e
10 32 25 50 43

Sami Mestiri How to use the R software


Creating a matrix

J The function cbind() (column bind)create the


matrix from vectors in columns :
x = 1 :5
y=x*2
A = cbind (x, y)
x y
[1] 1.00 2.00
[2] 2.00 4.00
[3] 3.00 6.00
[4] 4.00 8.00
[5] 5.00 10.00

Sami Mestiri How to use the R software


Creating a matrix

J The function rbind() (row bind) create the


matrix from vectors in lines :
x = 1 :5
y=x*2
B = rbind(x, y)
[, 1] [, 2] [, 3] [, 4] [, 5]
x 1.00 2.00 3.00 4.00 5.00
y 2.00 4.00 6.00 8.00 10.00

Sami Mestiri How to use the R software


Creating a matrix

J The function matrix() create a matrix, specifying


in arguments the number of lines :
M = matrix (c(1,2,3,4,5,6,7,8,9,10), nrow = 2, ncol
= 5)
[, 1] [, 2] [, 3] [, 4] [, 5]
x 1.00 3.00 5.00 7.00 9.00
y 2.00 4.00 6.00 8.00 10.00

Sami Mestiri How to use the R software


Operations on matrices

J Dimension : dim(mat) given the row number and


column number of the matrix.
Example : dim(M)
J The product of two matrices is written with the
operator%*%.
mat1 = matrix(1 :10,ncol=2)
mat2 = matrix(1 :10,nrow=2,byrow=T)
mat = mat1 %*% mat2
mat

Sami Mestiri How to use the R software


Creating a factor

J The function factor() create a vector composed


by the modalities of a variable qualitative or discrete).
J The function as.factor() is used to convert a
digital vector into a factor.
v = c(1, 2, 1, 2, 1)
a = as.factor(v)
f = factor(v, labels=c("yes","no"))
f
[1] yes no yes no yes
Levels : yes no

Sami Mestiri How to use the R software


Creating a data frame

J The function data.frame () allows to group, or


concatenate, within the same object :
(i) the vector of the measured variable (s)
(dependent),
(ii) classification vectors (factors),
(iii) other data matched to the previous ones (e.g.
name or n subject).
J The data.frame elements will be accessible using
the $ operator next to the data.frame name. The
only constraint is that the vectors must be of the
same size.

Sami Mestiri How to use the R software


The data.frame

J We will create a small database with the names,


salaries and start dates of some employees :
employe = c("Ali bn Salh ","Afef Hmida","fayza bn
amor")
salaire = c(21000, 23400, 26800)
startdate =
as.Date(c("2010-11-1","2008-3-25","2007-3-14"))
Tab = data.frame(employe, salaire, startdate)
Tab

Sami Mestiri How to use the R software


Objects in memory

J The function ls() list objects in memory.


ls()
J Clear objects in memory
rm(a,b)
J Clear all objects
rm(list=ls())

Sami Mestiri How to use the R software


Ask for help

J Learn more about a function


help.search("title")
?mean
J How to import text files ?
?read.table

Sami Mestiri How to use the R software


THANK YOU FOR YOUR ATTENTION

View publication stats Sami Mestiri How to use the R software

You might also like