|
| 1 | +--- |
| 2 | +title: Brief recap of sessions 1 & 2 |
| 3 | +--- |
| 4 | + |
| 5 | +## Data types in R |
| 6 | + |
| 7 | + |
| 8 | +## Data structures in R |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Atomic vector |
| 13 | + |
| 14 | +```{r} |
| 15 | +x <- 100 |
| 16 | +``` |
| 17 | + |
| 18 | +### Atomic vectors with multiple values |
| 19 | + |
| 20 | +```{r} |
| 21 | +x <- c("apple", "banana", "mango") |
| 22 | +y <- c(12, 45, 87, 90) |
| 23 | +z <- 1:100 |
| 24 | +``` |
| 25 | + |
| 26 | +#### Using variables in operations |
| 27 | + |
| 28 | +```{r} |
| 29 | +y * 5 |
| 30 | +
|
| 31 | +z + y |
| 32 | +``` |
| 33 | + |
| 34 | +```{r error=TRUE} |
| 35 | +x + y |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +#### Accessing particular values |
| 40 | + |
| 41 | +```{r} |
| 42 | +x[2] |
| 43 | +y[2:3] |
| 44 | +z[ z > 23 & z < 56] |
| 45 | +``` |
| 46 | + |
| 47 | +## Functions |
| 48 | + |
| 49 | +```{r} |
| 50 | +round(pi, digits = 4) |
| 51 | +``` |
| 52 | + |
| 53 | +## factors |
| 54 | + |
| 55 | +```{r} |
| 56 | +x <- c("low", "high", "medium", "high", "low", "medium", "high") |
| 57 | +response <- factor(x) |
| 58 | +response |
| 59 | +response <- factor(x, levels = c("low", "medium", "high")) |
| 60 | +response |
| 61 | +``` |
| 62 | + |
| 63 | +## matrices |
| 64 | + |
| 65 | +```{r} |
| 66 | +y <- matrix(data = 1:12, nrow = 4, ncol = 3) |
| 67 | +dim(y) |
| 68 | +ncol(y) |
| 69 | +nrow(y) |
| 70 | +
|
| 71 | +y[2, 3] |
| 72 | +y[2:3, ] |
| 73 | +``` |
| 74 | + |
| 75 | +## Lists |
| 76 | + |
| 77 | +```{r} |
| 78 | +cities <- list( |
| 79 | + city = c("Kampala", "London", "Paris"), |
| 80 | + population = c(1.51, 8.8, 2.1) |
| 81 | +) |
| 82 | +
|
| 83 | +cities |
| 84 | +``` |
| 85 | + |
| 86 | +### Accessing list elements |
| 87 | + |
| 88 | +```{r} |
| 89 | +cities[[2]] |
| 90 | +cities$city |
| 91 | +``` |
| 92 | + |
| 93 | +### Modifying the list |
| 94 | + |
| 95 | +Modify a specific entr |
| 96 | + |
| 97 | +```{r} |
| 98 | +cities$city[2] <- "New York" |
| 99 | +``` |
| 100 | + |
| 101 | +Add a new element |
| 102 | + |
| 103 | +```{r} |
| 104 | +cities$country <- c("Uganda", "USA", "France") |
| 105 | +cities |
| 106 | +``` |
| 107 | + |
| 108 | +## Data Frames |
| 109 | + |
| 110 | +```{r} |
| 111 | +dat <- data.frame( |
| 112 | + city = c("Kampala", "London", "Paris"), |
| 113 | + population = c(1.51, 8.8, 2.1) |
| 114 | +) |
| 115 | +dat |
| 116 | +``` |
| 117 | + |
| 118 | +### Accessing records |
| 119 | + |
| 120 | +```{r} |
| 121 | +dat[1:2, ] |
| 122 | +dat$city[2] |
| 123 | +dat[dat$population > 2, "city"] |
| 124 | +``` |
0 commit comments