|
| 1 | +--- |
| 2 | +title: "R recap" |
| 3 | +author: "Your Name Here" |
| 4 | +date: '27th April' |
| 5 | +output: pdf_document |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +## Getting started |
| 10 | + |
| 11 | +First load the wakefield package |
| 12 | + |
| 13 | +```{r} |
| 14 | +library(wakefield) |
| 15 | +``` |
| 16 | + |
| 17 | +Now run this function |
| 18 | + |
| 19 | +```{r} |
| 20 | +random_patients <- function(n) { |
| 21 | + as.data.frame(r_data_frame( |
| 22 | + n, |
| 23 | + id, |
| 24 | + name, |
| 25 | + race, |
| 26 | + sex, |
| 27 | + smokes, |
| 28 | + height, |
| 29 | + birth(random = TRUE, x = NULL, start = Sys.Date() - 365 * 45, k = 365*2,by = "1 days"), |
| 30 | + state, |
| 31 | + pet, |
| 32 | + grade_level(x=1:3), |
| 33 | + died, |
| 34 | + normal(name="Count"), |
| 35 | + date_stamp) |
| 36 | + ) |
| 37 | +} |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +```{r} |
| 44 | +patients <- random_patients(10) |
| 45 | +``` |
| 46 | + |
| 47 | +```{r} |
| 48 | +View(patients) |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +****** |
| 53 | + |
| 54 | +## Q. What are the dimensions of the data frame? |
| 55 | + |
| 56 | +## Q. What columns are available? |
| 57 | + |
| 58 | +*** HINT: see the `dim`, `ncol`, `nrow` and `colnames` functions |
| 59 | + |
| 60 | +****** |
| 61 | + |
| 62 | + |
| 63 | +```{r } |
| 64 | +``` |
| 65 | + |
| 66 | +****** |
| 67 | +## Q. Can you think of two ways to access the Names of the patients? |
| 68 | +## Q. What type of object is returned? |
| 69 | + |
| 70 | +****** |
| 71 | +```{r} |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## Subsetting |
| 77 | + |
| 78 | + |
| 79 | +****** |
| 80 | +## Q. Make sure you can understand the behaviour of the following commands |
| 81 | +****** |
| 82 | + |
| 83 | +```{r} |
| 84 | +patients[1,2] |
| 85 | +patients[2,1] |
| 86 | +patients[c(1,2,3),1] |
| 87 | +patients[c(1,2,3),c(1,2,3)] |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +****** |
| 92 | +## Q. Make sure you can understand the behaviour of the following commands |
| 93 | +****** |
| 94 | + |
| 95 | + |
| 96 | +```{r} |
| 97 | +patients[1,] |
| 98 | +patients[,1] |
| 99 | +patients[,c(1,2)] |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +****** |
| 105 | +## Q. Can you create a data frame of dog owners? |
| 106 | + |
| 107 | +****** |
| 108 | + |
| 109 | + |
| 110 | +```{r} |
| 111 | +
|
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +****** |
| 116 | +## Q. Can you create a data frame of deceased patients with a 'count' < 0 |
| 117 | + |
| 118 | +****** |
| 119 | + |
| 120 | +```{r} |
| 121 | +
|
| 122 | +``` |
| 123 | + |
| 124 | +****** |
| 125 | +## Q. Create a data frame where the patients are arranged in decreasing height order |
| 126 | + |
| 127 | +****** |
| 128 | + |
| 129 | +## Simple plotting |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +```{r} |
| 134 | +hist(patients$Height) |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +```{r} |
| 139 | +plot(patients$Height,patients$Count) |
| 140 | +``` |
| 141 | + |
| 142 | + |
| 143 | +```{r} |
| 144 | +barplot(table(patients$Race)) |
| 145 | +``` |
| 146 | + |
| 147 | +```{r} |
| 148 | +barplot(table(patients$Pet)) |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +```{r} |
| 153 | +boxplot(patients$Count ~ patients$Died) |
| 154 | +``` |
| 155 | + |
| 156 | + |
| 157 | +```{r} |
| 158 | +plot(patients$Height,patients$Count,pch=16, |
| 159 | + col="red",xlab="Height", |
| 160 | + ylab="Count") |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +```{r} |
| 165 | +boxplot(patients$Count ~ patients$Died,col=c("red","yellow")) |
| 166 | +``` |
| 167 | + |
| 168 | +****** |
| 169 | +## Make the following plots |
| 170 | +## 1. Histogram of the Count variable |
| 171 | +## 2. Barplot of the frequency of pet ownership |
| 172 | +## 3. Boxplot of Height according to smoker / non-smoker |
| 173 | +## ...anything else that takes your fancy |
| 174 | + |
| 175 | +****** |
| 176 | + |
| 177 | +```{r} |
| 178 | +png("myLittlePlot.png") |
| 179 | +barplot(table(patients$Pet)) |
| 180 | +dev.off() |
| 181 | +``` |
0 commit comments