Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
by R-Tutorials Training
4. Help Functionalities
help.start()
?hist
apropos("hist")
example(hist)
vignette()
# shows supporting material
vignette("grid")
5. R as a giant calculator
4+ 4+
- operators: +,-,*,/
5+6-4/2*5
- remove objects
rm("x")
sum(x)
sqrt(x)
6. Types of brackets
7. Data Sequences
seq(3, 5)
seq(from = 3, to = 5)
o to repeat sequences
rep(c(3,4,5) , 3)
rep(1:10, times = 3)
x <- c(1,2,3)
rep(x, each = 3)
rep(x, each = 3, times = 3)
x = c(4:20)
which(x == 10) # note the 2 equal signs, used for logical operations
o reverse of
x[3]
1. Define the object "myobject" and assign the vector 1:10 in at least 3
different ways
Solutions
1. Define the object "myobject" and assign the vector 1:10 in at least 3
different ways
sum (myobject)
x[7]
8. New Datasets
?lynx
head(lynx)
head(iris)
tail(lynx)
summary(lynx)
o or we can use the attach argument to make handling this set easier
attach(iris)
sum(Sepal.Length)
9. Functions in R
t= 5
z= 9
mysecondfn(t,z)
10.
Loops - loops and functions are a crucial part in
programming
# we use any to check that i (of this loop round) is still in s, multiples of i
will be removed
if(any(s==i)){
# we store i if it meets our criteria in p together with the previous p
p = c(p,i)
# to search for numbers with a remainder at modulus division
s = c(s[(s%%i) != 0],i)
}}
return(p) }
# to specify the output if n < 2 (optional)
else{
stop("Input at least 2")
}}
PrimVec(100)
11.
Graphs in R
or by using a dataset
plot(lynx)
plot(lynx, main="Lynx Trappings", col="red",
col.main=52, cex.main=1.5)
o the cex family can be used to change magnification factors
par(mfrow=c(2,2), col.axis="red")
colors
colors() # huge variety
Line Types
par(mfrow=c(1,1), col.axis="black")
library(plotrix)
plot(1:6, ylab="", main="Line Types lty 1:6", xlab="lty 1:6")
ablineclip(v=1, lty=1, col="sienna2", lwd=2)
ablineclip(v=2, lty=2, col="sienna2", lwd=2)
ablineclip(v=3, lty=3, col="sienna2", lwd=2)
ablineclip(v=4, lty=4, col="sienna2", lwd=2)
ablineclip(v=5, lty=5, col="sienna2", lwd=2)
ablineclip(v=6, lty=6, col="sienna2", lwd=2)
ablineclip(v=7, lty=0, col="sienna2", lwd=2)
Types of plots
o by using "type" we can specify which kind of plot we want
Example: by skilled usage of all the available parameters you can create
quite complex graphs
par(mar=c(4,3,3,3), col.axis="black")
plot(cars$speed, type="s", col="red", bty="n", xlab="Cars ID", ylab="")
text(8, 14, "Speed in mph", cex=0.85, col="red") # this adds the
explanatory text to plot 1
par(new=T) # allows 2 in one plot
graphical parameters
par()
?par
Check out the Graphs in R course to learn about every single par Redeem the code happy29 to get it for $29 and save 70%
12.
13.
Exercise - R Graphs
Solution
?rivers
x = 1:141
y= rivers
plot(x,y, col = "green", pch = 20,
main = "Lengths of\nMajor N. American Rivers",
col.main ="red", xlab = "",
ylab = "length in miles")