Download R from the web site: https://r-project.org/
-> go to Download R for Windows/Linux/Mac -> Install R for the first time ->
-> Download R 3.6.2 for Windows (for example)
On the same web site https://r-project.org/ -> Manuals and "contributed documentation"
Also Packages -> Table of available packages, sorted by name (15330 available packages)
R, also called the Language for Statistical Computing, was developed by Ross Ihaka and
Robert Gentleman at the University of Auckland in the nineties. It is considered an open
source implementation of the S language, which was developed by John Chambers in the Bell
Laboratories in the eighties.
Advantages of R:
- open source => free!
- R's graphical capabilities are top notch
- easy to build publication quality plots
- R uses a command line interface
- code things in your console and in scripts => work is reproducible
- large audience both in academia and in business
- easy to create R packages (extensions of R) aimed at solving particular problems
- R has a very active community that has created thousands of well-documented R packages
for a very broad range of applications in the financial sector, health care and for cutting
edge research.
Dezadvantages of R:
- easy to learn, hard to master
- steep learning curve
R console -> Menus: File (scripts), Packages, Help
#Getting help
- ??name
??data.frame
??mean
help(mean)
help(package="MASS")
example(lm)
demo(package="stats")
demo("nlm", package="stats")
apropos("glm")
help.search("^glm") # ??"glm"
RSiteSearch("foo") # search for foo in help manuals and archived mailing lists
Web sites:
https://r-project.org
https://cran.r-project.org/doc/manuals/R-intro.html
https://stackoverflow.com/questions/tagged/r
https://www.statmethods.net
https://r-tutor.com
http://r-blogger.com
Installing packages
install.packages("ggplot2")
library(ggplot2)
#Simple arithmetic
2+3*5^(-1/2)
7%%3 # the remainder
7%/%3 # the integer division
#Math functions
- trigonometric functions: sin(), cos(), tan(), asin(), acos(), atan()
sinpi(), cospi(x), tanpi()
sin(pi/2)
atan(1)
- hyperbolic trigonometric functions: sinh, cosh, tanh, acosh, asinh, atanh
- other: abs, sign, sqrt, floor, ceiling, trunc, round, signif, exp, log, log10, log2, log1p
abs(-3)
sqrt(15)
log(1)
log10(100)
log(8,2)
exp(-2)
floor(1.2)
round(-0.2) #integer part
round(1.6)
sin(pi)
#Variables
- allows you to store a value or an object in R
- you can then later use this variable's name to easily access the value or the object that
is stored within this variable.
- use the "<-" symbol
a<-3
5->b
c=-7
a+3*b^c
ls() # list of created variables
rm(a) # remove variable "a"
ls()
#Data types a.k.a. atomic vector types
- logical: TRUE (T), FALSE (F)
- numeric: integer, double, complex
- character
class() #gives the type of a variable
class(b)
is.integer(b)
d=as.integer(b) # data type coercion
class(d)
#Vectors
- c() for creating a vector
- all the elements have the same type
- simple variables are vectors with length 1
v1<-c(1,3,5,-9)
v1
v2=c("ana","are","mere")
is.vector(v2)
names(v1)=c("val1","val2","val3","val4") # give names to the values of a vector
v1
v3<-c("diamonds"=11,"clubs"=5,"hearts"=10,"spades"=13)
v3
length(v3) # length of a vector
sum(v3)
v4<-c(1,"aa",-2,3.5,"xx")
v4 # all elements have been transformed to type character
class(v4)
v4=c(v4,"aaa")
Vector arithmetic:
- operations for vectors will be applied element-by-element
a=1:10
b=12:3
c=seq(1,11,2)
c
a+2*b
b^2
-5*a
sum(a) # sum of all elements of a vector
a**5]
b[c(T,F,T,T)] # recycling
v1["val2"]
Further reading: https://cran.r-project.org/doc/manuals/R-intro.html#Simple-manipulations-numbers-and-vectors
#Matrices
m=matrix(1:9,nrow=3,byrow=T)
m
m1=1:7
m2=3:9
a=cbind(m1,m2)
b=rbind(m1,m2)
a
b
#Data frames
c=rep("FF",7)
d=data.frame(m1,m2,c)
d
#Programming with R
Conditional execution: if statements
if (expr_1) expr_2 else expr_3
- operators: &&, ||
a=2;b=-5
if (a>b) {a=a+5;c=b-a} else c=b+a
a;c
d=ifelse(a>b,-6,7)
d
Repetitive execution: for loops, repeat and while
for (name in expr_1) expr_2
for (i in 1:5) print(i^2)
Other looping facilities include the
repeat expr
while (condition) expr
The break statement can be used to terminate any loop, possibly abnormally.
This is the only way to terminate repeat loops.
The next statement can be used to discontinue one particular cycle and skip to the “next”.
a=5
while (a>=0) {print(a);a=a-1}
a=5
repeat {print(a);a=a-1;if (a<0) break;}
#User defined functions
name <- function(arg_1, arg_2, …) expression
ssum<- function(a,b)
{if (a>b) return(a+b) else return(a-b)}
ssum(2,3)
ssum(3,2)#comment
Writing on multiple lines: + symbol appears
mean(v1
)
CTRL+L clears the R Console of all the previous commands
**