Prof. Eric A. Suess
So how should you complete your homework for this class?
- First thing to do is type all of your information about the problems you do in the text part of your R Notebook.
- Second thing to do is type all of your R code into R chunks that can be run.
- If you load the tidyverse in an R Notebook chunk, be sure to include the “message = FALSE” in the {r}, so {r message = FALSE}.
- Last thing is to spell check your R Notebook. Edit > Check Spelling… or hit the F7 key.
Homework 1
Read: Preface, Chapter 1, Appendix A, B, and D.
Problems:
Appendix B Exercises B.1, B.2, B.3, B.4
Appendix D Exercises D.1, D.6, D.11
Appendix B
B.1
We see nothing. Well actually we see the first layer of a ggplot2 plot.
obj1 <- 2:10
obj2 <- c(2, 5)
obj3 <- c(TRUE, FALSE)
obj4 <- 42
obj1 * 10
[1] 20 30 40 50 60 70 80 90 100
obj1[2:4]
[1] 3 4 5
obj1[-3]
[1] 2 3 5 6 7 8 9 10
obj1 + obj2
longer object length is not a multiple of shorter object length
[1] 4 8 6 10 8 12 10 14 12
obj1 * obj3
longer object length is not a multiple of shorter object length
[1] 2 0 4 0 6 0 8 0 10
obj1 + obj4
[1] 44 45 46 47 48 49 50 51 52
obj2 + obj3
[1] 3 5
sum(obj2)
[1] 7
sum(obj3)
[1] 1
B.2
a <- c(10, 15)
b <- c(TRUE, FALSE)
c <- c("happy", "sad")
data.frame(a, b, c)
cbind(a, b)
a b
[1,] 10 1
[2,] 15 0
rbind(a, b)
[,1] [,2]
a 10 15
b 1 0
cbind(a, b, c)
a b c
[1,] "10" "TRUE" "happy"
[2,] "15" "FALSE" "sad"
list(a, b, c)[[2]]
[1] TRUE FALSE
B.3
mylist <- list(x1="sally", x2=42, x3=FALSE, x4=1:5)
is.list(mylist)
[1] TRUE
names(mylist)
[1] "x1" "x2" "x3" "x4"
length(mylist)
[1] 4
mylist[[2]]
[1] 42
mylist[["x1"]]
[1] "sally"
mylist$x2
[1] 42
length(mylist[["x4"]])
[1] 5
class(mylist)
[1] "list"
typeof(mylist)
[1] "list"
class(mylist[[4]])
[1] "integer"
typeof(mylist[[3]])
[1] "logical"
B.4
library(mosaic)
ds <- read.csv("http://nhorton.people.amherst.edu/r2/datasets/helpmiss.csv")
ds
Describe in words what computations are being done. Using the “pipe” notation, trans- late this code into a more readable version.
summarise(group_by(select(filter(mutate(ds,
sex = ifelse(female==1, "F", "M")), !is.na(pcs)), age, pcs, sex),
sex), meanage=mean(age), meanpcs=mean(pcs),n=n())
Answer of exercise B.4
This code generates a new variable called sex based on the value of the female variable, drops any observations missing the pcs variable, selects three variables, then displays the average age, PCS, and sample size for each of the two groups defined by sex.
Here it is much clearer what each operation within the “pipe-stream” is doing. It is straightforward to debug expressions in this manner by just leaving off the %>% at each line: this will only evaluate the set of functions called to that point and display the intermediate output.
library(mosaic)
ds <-
read.csv("http://nhorton.people.amherst.edu/r2/datasets/helpmiss.csv")
ds %>% mutate(sex = ifelse(female==1, "F", "M")) %>%
filter(!is.na(pcs)) %>%
select(age, pcs, sex) %>%
group_by(sex) %>%
summarise(meanage = mean(age), meanpcs = mean(pcs), count = n())
B.5
Answers may vary.
library(readr) # package, function call, argument
ds <- read_csv("http://www.mysite.com/myfile", progress = TRUE)
# quoted character string, named argument
Appendix D
D.1
x <- 1:5
x <- x + 1
x
[1] 2 3 4 5 6
D.6
The implication is that arbitrary LATEX commands can be included in R Markdown files to format mathematical expressions.
\(\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot x + \epsilon\)
D.11
library(xtable)
library(mdsr)
options(xtable.comment = FALSE)
mod <- lm(cesd ~ mcs + sex, data = HELPrct)
xtable(mod)
LS0tCnRpdGxlOiAnU3RhdC4gNjUwIFNlY3Rpb24gMSBvciAyOiBIb21ld29yayAxJwpvdXRwdXQ6CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCi0tLQoKKipQcm9mLiBFcmljIEEuIFN1ZXNzKioKClNvIG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