279x Filetype PDF File size 1.52 MB Source: www.westernsydney.edu.au
ALearningGuidetoR
Solutions to Exercises
August,
Contents
Basics of R
.
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Calculating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Simpleobjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Working with a single vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
To quote or not to quote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Working with two vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Alphabet aerobics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Comparingandcombiningvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. nto the matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Save your work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generating,readingandextractingdata
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Workingwithasinglevector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Alphabetaerobics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Basic operations with the Cereal data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Ashortdataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
Titanic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Managingyourworkspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specialdatatypes
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Titanic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Hydrodam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. HFEtreemeasurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Flux data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
AlphabetAerobics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. DNAAerobics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Visualizingdata
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Scatter plot with the pupae data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Flux data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Hydrodam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Colouredscatter plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
Superimposedhistograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Trellis graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basicstatistics
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Univariate distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Moret-tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Simplelinear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
Quantile Quest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summarizing,tabulatingandmergingdata
.
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Summarizingthecerealdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Wordsandtheweather . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Mergenewdataontothepupaedata . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Mergingmultipledatasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
Orderedboxplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Variances in the x F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. Weightloss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Linearmodelling
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. One-wayANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Two-wayANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Multiple regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Linear Model with factor and numeric variables . . . . . . . . . . . . . . . . . . . . . . .
..
Logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Generalized linear model (GLM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions,listsandloops
. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Workingwithlists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Usingfunctions to make manyplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. Monthlyweatherplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
TheCentrallimit theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter1
Basics of R
1.15 Exercises
n these exercises, we use the following colour codes:
∎ Easy: make sure you complete some of these before moving on. These exercises will follow
examplesinthetextveryclosely.
Æntermediate: a bit harder. You will often have to combine functions to solve the exercise in
twosteps.
▲Hard: difficult exercises! These exercises will require multiple steps, and significant departure
fromexamplesinthetext.
Wesuggestyoucompletetheseexercises in an R markdown file. This will allow you to combine code
chunks, graphical output, and written answers in a single, easy-to-read file.
1.15.1 Calculating
Calculate the following quantities:
. ∎ The sum of 100.1, 234.9 and 12.01
100.1 + 234.9 + 12.01
## [1] 347.01
. ∎ The square root of 256
sqrt(256)
## [1] 16
. ∎ Calculate the -based logarithm of 100, and multiply the result with the cosine of π. Hint: see
?logand?pi.
log10(100)*cos(pi)
## [1] -2
. ∎ Calculate the cumulative sum (’running total’) of the numbers ,,,
,.
no reviews yet
Please Login to review.