This document introduces you to R Markdown, showing how you might write up a homework assignment.
We had to determine the type of three different vectors.
Determine the type of each of the following vectors:
c(3.2, 2L, 4.7, TRUE) This will be a double. In the coercion process, logical is beaten by integer, which in turn is beaten by double.c(as.integer(3.2), 2L, 5L, TRUE) This will be integer. Integers beat logical.c(as.integer(3.2), 2L, "5L", TRUE) This will be a character vector. The string `“5L” is of type character, and character beats every other type.Note: I checked my answers with R’s typeof() function. For example:
typeof(c(3.2, 2L, 4.7, TRUE))
## [1] "double"
Using a combination of c(), rep() and seq() and other operations, find concise one-line programs to produce each of several target vectors:
For the first vector, I did:
seq(4, 307, by = 3) # one way
## [1] 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52
## [18] 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100 103
## [35] 106 109 112 115 118 121 124 127 130 133 136 139 142 145 148 151 154
## [52] 157 160 163 166 169 172 175 178 181 184 187 190 193 196 199 202 205
## [69] 208 211 214 217 220 223 226 229 232 235 238 241 244 247 250 253 256
## [86] 259 262 265 268 271 274 277 280 283 286 289 292 295 298 301 304 307
For the second vector, I did:
seq(0.01, 0.99, by = 0.01) # one way
## [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14
## [15] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28
## [29] 0.29 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42
## [43] 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56
## [57] 0.57 0.58 0.59 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70
## [71] 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84
## [85] 0.85 0.86 0.87 0.88 0.89 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98
## [99] 0.99
For the third vector, I did:
rep(seq(2,12, by = 2), each = 12)
## [1] 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4
## [24] 4 6 6 6 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 8
## [47] 8 8 10 10 10 10 10 10 10 10 10 10 10 10 12 12 12 12 12 12 12 12 12
## [70] 12 12 12
Here’s my code to produce the fourth vector:
rep(1:10, times = 1:10)
## [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7
## [24] 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10
## [47] 10 10 10 10 10 10 10 10 10
The following three vectors gives the names, heights and ages of five people, and also say whether or not each person likes Toto:
person <- c("Akash", "Bee", "Celia", "Devadatta", "Enid")
age <- c(23, 21, 22, 25, 63)
height <- c(68, 67, 71, 70, 69)
likesToto <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
I had to use-sub-setting to get four different results.
the names of all people over the age of 22;
person[age > 22]
## [1] "Akash" "Devadatta" "Enid"the names of all people younger than 24 who are also more than 67 inches tall;
person[age < 24 & height > 67]
## [1] "Akash" "Celia"the names of all people who either don’t like Toto or who are over the age of 30;
person[!likesToto | age > 30]
## [1] "Celia" "Devadatta" "Enid"
Note: On this part I got some help from my fellow student Bettina Lauf, who pointed out that !likesToto is quicker than likesToto == FALSE, though the latter would ge tthe job done, too.
the number of people who are over the age of 22.
length(person[age > 22])
## [1] 3We had to use sum() find how many people had particular characteristics.
the number of people younger than 24 who are also more than 67 inches tall;
sum(age < 24 & height > 67)
## [1] 2the number of people who either don’t like Toto or who are over the age of 30.
sum(!likesToto | age > 30)
## [1] 3If you want italicized text, surround it with a dingle asterisk.
You get boldface when you surround text with two asterisks.
Unordered lists can be make like this:
You can make list within lists (just indent four spaces)
If you want to emphasize text, consider a blockquote:
This could be a really important idea. It appears in a blockquote. You can highlight an entire paragraph if you jyst put a
>at the very beginning of the line, immediately followed by your text.
Note that “computer code” in a line is produced with back-ticks, as in cat("Meow").
You can display some R code without actually running it if you set the “chunk option” eval = FALSE:
cat(Hello World) # not running this, as it would produce an erro
You can run R code without showing the code itself in the knitted document. Just use the chunk option echo = FALSE. For example, here comes a vector:
## [1] 2 4 6 8 10 12 14 16 18 20
Later on you’ll make graphs. Here’s a sample:
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(color = cyl)) +
geom_smooth(se = FALSE, color = "red") +
labs(title = "Cars with bigger engines usually are less fuel-efficient",
x = "engine displacement (cubic inches)",
y = "fuel efficiency (miles per gallon)")
## `geom_smooth()` using method = 'loess'
This is my caption: ‘Wow, a graph!’