R Module 2
R Module 2
• You can see the available built-in data in R from this website:
https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/
Introduction of Function
• Functions in R is determined by sign “()” for the input. Most of it has
more than 1 input, for example:
seq(from=1, to=15, by=2)
## [1] 1 3 5 7 9 11 13 15
• There are tons of packages including outsourcing packages for certain
occasions, the next fews module will comprehensively explain about
it.
Introduction of Function
• Regular Sequence
The function seq can generate sequences of real numbers as follows:
seq(10,200,5)
[1] 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115
[23] 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200
• Replication
> rep(1,4)
[1] 1 1 1 1
Introduction to Function
• The function gl (generate levels) is very useful because it generates regular series of
factors. The usage of this fonction is gl(k, n) where k is the number of levels (or
classes), and n is the number of replications in each level. Two options may be used:
length to specify the number of data produced, and labels to specify the names of the
levels of the factor. Examples:
> gl (2,5)
[1] 1 1 1 1 1 2 2 2 2 2
Levels: 1 2
> gl (2,5,length=10)
[1] 1 1 1 1 1 2 2 2 2 2
Levels: 1 2
> gl (2,5,length=11)
[1] 1 1 1 1 1 2 2 2 2 2 1
Levels: 1 2
Vectors
• The properties of vectors
1. Vectors are homogeneous (have the same type;)
2. Vectors can be indexed by position;
v[2] refers to the second element of v