Some hints:
Functions to look up (Technically, women is a default dataset in R)
?seq
?"["
?women
Code examples
Note that this output has '#'es before it for ease of copying and pasting.
# Generate sequence from 1 to 10 in steps of 2
seq(from = 1, to = 10, by = 2)
## [1] 1 3 5 7 9
# Show the dataframe `women` that comes with R
women
## height weight
## 1 58 115
## 2 59 117
## 3 60 120
## 4 61 123
## 5 62 126
## 6 63 129
## 7 64 132
## 8 65 135
## 9 66 139
## 10 67 142
## 11 68 146
## 12 69 150
## 13 70 154
## 14 71 159
## 15 72 164
# Show the first row of women
women[1, ]
## height weight
## 1 58 115
# Show the first column of women
women[, 1]
## [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# Show the column of women titled 'height'
women[, "height"]
## [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# Get the 2nd, 3rd and 7th rows
women[c(2, 3, 7), ]
## height weight
## 2 59 117
## 3 60 120
## 7 64 132
# Show women without the 2nd, 3rd and 7th row.
women[-c(2, 3, 7), ]
## height weight
## 1 58 115
## 4 61 123
## 5 62 126
## 6 63 129
## 8 65 135
## 9 66 139
## 10 67 142
## 11 68 146
## 12 69 150
## 13 70 154
## 14 71 159
## 15 72 164
# How many rows and columns does the data have?
nrow(women)
## [1] 15
ncol(women)
## [1] 2
# Get the last row in the first column
women[nrow(women), 1]
## [1] 72
Your mission
Take this dataframe:
dat <- data.frame(a = c(1, 2, 4, 5, 2), b = c(3, 5, 7, 34, 3), c = c(7, 2, 3,
6, 7))
dat
## a b c
## 1 1 3 7
## 2 2 5 2
## 3 4 7 3
## 4 5 34 6
## 5 2 3 7
And get every odd-numbered row of the third column:
## [1] 7 3 7