There are numerous ways to generate vectors which have some structure. Here are some useful commands:
The easiest way to do this is to use the c (concatenate) command:
x <- c(0, 2, 3, 1, 5)
x
## [1] 0 2 3 1 5
to make regular sequences use “:” (read “to”)
1:5
## [1] 1 2 3 4 5
-2:2
## [1] -2 -1 0 1 2
and then we can combine these:
c(1:5, 11:15)
## [1] 1 2 3 4 5 11 12 13 14 15
there are also a number of commands for this purpose:
seq(1, 10, 1)
## [1] 1 2 3 4 5 6 7 8 9 10
seq(1, 10, 1/2)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5
## [15] 8.0 8.5 9.0 9.5 10.0
seq(0, 10, length=20)
## [1] 0.0000000 0.5263158 1.0526316 1.5789474 2.1052632 2.6315789
## [7] 3.1578947 3.6842105 4.2105263 4.7368421 5.2631579 5.7894737
## [13] 6.3157895 6.8421053 7.3684211 7.8947368 8.4210526 8.9473684
## [19] 9.4736842 10.0000000
this creates a series of sequences of integers each ending by the numbers given as arguments:
sequence(10)
## [1] 1 2 3 4 5 6 7 8 9 10
sequence(c(2, 5, 3))
## [1] 1 2 1 2 3 4 5 1 2 3
rep(1, 10)
## [1] 1 1 1 1 1 1 1 1 1 1
rep(1:3, 10)
## [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
rep(1:3, each=3)
## [1] 1 1 1 2 2 2 3 3 3
rep(c("A", "B", "C"), c(4, 7, 3))
## [1] "A" "A" "A" "A" "B" "B" "B" "B" "B" "B" "B" "C" "C" "C"
rep(1:3, rep(5, 3))
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
Exercise
What does this do?
rep(1:10, 10:1)
The function gl (generate levels) is very useful because it generates regular series of factors. The usage of this function 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.
gl(3, 5)
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
## Levels: 1 2 3
gl(3, 5, length=30)
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
## Levels: 1 2 3
gl(2, 6, label=c("Male", "Female"))
## [1] Male Male Male Male Male Male Female Female Female Female
## [11] Female Female
## Levels: Male Female
this takes a couple of vectors and writes them as a matrix with each combination.
expand.grid(1:2, 1:3)
## Var1 Var2
## 1 1 1
## 2 2 1
## 3 1 2
## 4 2 2
## 5 1 3
## 6 2 3
expand.grid(First=1:2, Second=1:3, Third=c("A", "B"))
## First Second Third
## 1 1 1 A
## 2 2 1 A
## 3 1 2 A
## 4 2 2 A
## 5 1 3 A
## 6 2 3 A
## 7 1 1 B
## 8 2 1 B
## 9 1 2 B
## 10 2 2 B
## 11 1 3 B
## 12 2 3 B
there are a number of R routines who need the data in this format as arguments, so this is an easy way to convert them.
This calculates the outer product of two vectors. It creates a matrix of length(x) x length(y) where \(z_{ij} = f(x_i, y_j)\) for some function \(f\)
x <- 1:3
y <- 1:5
outer(x, y, "*")
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 2 4 6 8 10
## [3,] 3 6 9 12 15
You can use any function you like:
outer(x, y, function(x,y) {x^2/y})
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0.5 0.3333333 0.25 0.2
## [2,] 4 2.0 1.3333333 1.00 0.8
## [3,] 9 4.5 3.0000000 2.25 1.8
If it is actual multiplication you want there is also a short hand:
x %o% y
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 2 4 6 8 10
## [3,] 3 6 9 12 15
Want to practice your multiplication tables?
x <- 1:10
names(x) <- x
x %o% x
## 1 2 3 4 5 6 7 8 9 10
## 1 1 2 3 4 5 6 7 8 9 10
## 2 2 4 6 8 10 12 14 16 18 20
## 3 3 6 9 12 15 18 21 24 27 30
## 4 4 8 12 16 20 24 28 32 36 40
## 5 5 10 15 20 25 30 35 40 45 50
## 6 6 12 18 24 30 36 42 48 54 60
## 7 7 14 21 28 35 42 49 56 63 70
## 8 8 16 24 32 40 48 56 64 72 80
## 9 9 18 27 36 45 54 63 72 81 90
## 10 10 20 30 40 50 60 70 80 90 100
Exercise
What (if anything) does this do?
(1:3 %o% 1:3) %o% 1:2
Some common objects are easy to create:
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
up to now we discussed how to generate data objects. Soon we will be talking about how to write your own functions. There is however also a type of object somewhat in between, so called expressions.
An expression is a series of characters which make sense for R. All valid commands are expressions. When a command is typed directly on the keyboard, it is then evaluated by R and executed if it is valid. In many circumstances, it is useful to construct an expression without evaluating it: this is what the function expression is made for. It is, of course, possible to evaluate the expression subsequently with eval().
x <- 3; y <- 2.5; z <- 1
exp1 <- expression(x / (y + exp(z)))
exp1
## expression(x/(y + exp(z)))
eval(exp1)
## [1] 0.5749019
Expressions can be used for many things. Here are two examples:
curve(x^2*exp(-x^2), -2, 2,
main = expression(x^2*exp(-x^2)),
ylab = "")
D(exp1, "x")
## 1/(y + exp(z))
eval(D(exp1, "x"))
## [1] 0.191634
D(exp1, "y")
## -(x/(y + exp(z))^2)
eval(D(exp1, "y"))
## [1] -0.1101707
D(exp1, "z")
## -(x * exp(z)/(y + exp(z))^2)
eval(D(exp1, "z"))
## [1] -0.2994751
Exercise
Use the D function to find \(\frac{d^2}{dx^2} \frac{x^2}{1+x^3}|_{x=0}\)
In general R’s use as a symbolic language is very limited. There are of course many purpose built languages for this, such as Maple or Mathematica.