Vectors
- Create a variable called
x
and assign the following numeric values: 5, 8, 13, 4, 21, 30, 2, 11.
x <- c(5, 8, 13, 4, 21, 30, 2, 11)
- Extract the fifth element of
x
.
x[5]
## [1] 21
- Extract the first two and the last two elements of
x
.
x[c(1, 2, 7, 8)]
## [1] 5 8 2 11
- Extract all elements of
x
which are greater than 20.
x[x > 20]
## [1] 21 30
- Create a subset of
x
containing values smaller than 5 or greater than or equal to 13.
x[x < 5 | x == 13]
## [1] 13 4 2
Data frames
- Load and call up the built-in data set
OrchardSprays
.
data(OrchardSprays)
OrchardSprays
## decrease rowpos colpos treatment
## 1 57 1 1 D
## 2 95 2 1 E
## 3 8 3 1 B
## 4 69 4 1 H
## 5 92 5 1 G
## 6 90 6 1 F
## 7 15 7 1 C
## 8 2 8 1 A
## 9 84 1 2 C
## 10 6 2 2 B
## 11 127 3 2 H
## 12 36 4 2 D
## 13 51 5 2 E
## 14 2 6 2 A
## 15 69 7 2 F
## 16 71 8 2 G
## 17 87 1 3 F
## 18 72 2 3 H
## 19 5 3 3 A
## 20 39 4 3 E
## 21 22 5 3 D
## 22 16 6 3 C
## 23 72 7 3 G
## 24 4 8 3 B
## 25 130 1 4 H
## 26 4 2 4 A
## 27 114 3 4 E
## 28 9 4 4 C
## 29 20 5 4 F
## 30 24 6 4 G
## 31 10 7 4 B
## 32 51 8 4 D
## 33 43 1 5 E
## 34 28 2 5 D
## 35 60 3 5 G
## 36 5 4 5 A
## 37 17 5 5 C
## 38 7 6 5 B
## 39 81 7 5 H
## 40 71 8 5 F
## 41 12 1 6 A
## 42 29 2 6 C
## 43 44 3 6 F
## 44 77 4 6 G
## 45 4 5 6 B
## 46 27 6 6 D
## 47 47 7 6 E
## 48 76 8 6 H
## 49 8 1 7 B
## 50 72 2 7 G
## 51 13 3 7 C
## 52 57 4 7 F
## 53 4 5 7 A
## 54 81 6 7 H
## 55 20 7 7 D
## 56 61 8 7 E
## 57 80 1 8 G
## 58 114 2 8 F
## 59 39 3 8 D
## 60 14 4 8 B
## 61 86 5 8 H
## 62 55 6 8 E
## 63 3 7 8 A
## 64 19 8 8 C
- Create a new data frame called
dat
that only contains the decrease
and the treatment
variables.
dat <- OrchardSprays[, c("decrease", "treatment")]
dat
## decrease treatment
## 1 57 D
## 2 95 E
## 3 8 B
## 4 69 H
## 5 92 G
## 6 90 F
## 7 15 C
## 8 2 A
## 9 84 C
## 10 6 B
## 11 127 H
## 12 36 D
## 13 51 E
## 14 2 A
## 15 69 F
## 16 71 G
## 17 87 F
## 18 72 H
## 19 5 A
## 20 39 E
## 21 22 D
## 22 16 C
## 23 72 G
## 24 4 B
## 25 130 H
## 26 4 A
## 27 114 E
## 28 9 C
## 29 20 F
## 30 24 G
## 31 10 B
## 32 51 D
## 33 43 E
## 34 28 D
## 35 60 G
## 36 5 A
## 37 17 C
## 38 7 B
## 39 81 H
## 40 71 F
## 41 12 A
## 42 29 C
## 43 44 F
## 44 77 G
## 45 4 B
## 46 27 D
## 47 47 E
## 48 76 H
## 49 8 B
## 50 72 G
## 51 13 C
## 52 57 F
## 53 4 A
## 54 81 H
## 55 20 D
## 56 61 E
## 57 80 G
## 58 114 F
## 59 39 D
## 60 14 B
## 61 86 H
## 62 55 E
## 63 3 A
## 64 19 C
- Form a subset of
dat
that only contains treatment D.
dat[dat$treatment == "D", ]
## decrease treatment
## 1 57 D
## 12 36 D
## 21 22 D
## 32 51 D
## 34 28 D
## 46 27 D
## 55 20 D
## 59 39 D
- Form a subset of
dat
that only contains treatment D and observations where the response decrease
was larger than 30.
dat[dat$treatment == "D" & dat$decrease > 30, ]
## decrease treatment
## 1 57 D
## 12 36 D
## 32 51 D
## 59 39 D
- Form a subset of
dat
that only contains treatment D and observations where the response decrease
was between 30 and 50.
dat[dat$treatment == "D" & dat$decrease > 30 & dat$decrease < 50, ]
## decrease treatment
## 12 36 D
## 59 39 D