Read Chapter 2 to help you complete the questions in this exercise.
#
at the beginning of the line.
area_circle
.
If you can’t remember how to create and assign objects see Section
2.2 or watch this video.
Google is your friend if you can’t remember the formula to calculate the
area of a circle! Also, remember that R already knows about
pi
. Don’t worry if you’re stumped and feel free to ask one
of the instructors for guidance.
c()
to create a vector called weight
containing the weight (in
kg) of 10 children: 69, 62, 57, 59, 59, 64, 56, 66, 67, 66
(Section
2.3 or watch this video
for more information).
weight
vector.
Get R to calculate the mean, variance, standard deviation, range of
weights and the number of children of your weight
vector
(see Section
2.3 for more details). Now read Section 2.4 of the R book to
learn how to work with vectors. After reading this section you should be
able to extract the weights for the first five children using Positional
indexes and store these weights in a new variable called
first_five
. Remember, you will need to use the square
brackets [ ]
to extract (aka index, subset) elements from a
variable.
c()
function again to
create another vector called height
containing the height
(in cm) of the same 10 children:
112, 102, 83, 84, 99, 90, 77, 112, 133, 112
. Use the
summary()
function to summarise these data in the
height
object. Extract the height of the 2nd, 3rd, 9th and
10th child and assign these heights to a variable called
some_child
(take a look at the section Positional
indexes in the R book if you’re stuck). We can also extract elements
using Logical
indexes. Let’s extract all the heights of children less than or
equal to 99 cm and assign to a variable called
shorter_child
.
weight
and
height
variables to calculate the body mass index (BMI) for
each child. The BMI is calculated as weight (in kg) divided by the
square of the height (in meters). Store the results of this calculation
in a variable called bmi
. Note: you don’t need to do this
calculation for each child individually, you can use both vectors in the
BMI equation – this is called vectorisation (see Section 2.4.4
of the Introduction to R book).
seq()
function to create a sequence of numbers ranging from 0 to 1 in steps of
0.1 (this is also a vector by the way) and assign this sequence to a
variable called seq1
.
seq2
(Hint: you may find it useful to include the
rev()
function in your code).
rep()
function
to generate these sequences (see Section
2.3 for some clues):
height
you created in Q7.
Let’s sort the values of height
into ascending order
(shortest to tallest) and assign the sorted vector to a new variable
called height_sorted
. Take a look at Section 2.4.3 in the
R book to see how to do this. Now sort all heights into descending order
and assign the new vector a name of your choice.
child_name
with the following names of the 10 children:
"Alfred", "Barbara", "James", "Jane", "John", "Judy", "Louise", "Mary", "Ronald", "William"
.
order()
function in combination with the square
bracket notation [ ]
. Have a peep at Section 2.4.3 for
some details. Create a new variable called names_sort
to
store the names of the children ordered by child height (from shortest
to tallest). Who is the shortest? who is the tallest child? If you’re
not sure how to do this, please ask one of the instructors.
weight_rev
(Hint: perhaps include the rev()
function?). Who is the heaviest? Who is the lightest?
NA
. Missing data can be tricky to deal with in R (and in
statistics more generally) and cause some surprising behaviour when
using some functions. Take a look at Section 2.4.5 of the
R book for more information about missing values. To explore this a
little further let’s create a vector called mydata
with the
values 2, 4, 1, 6, 8, 5, NA, 4, 7
. Notice the value of the
7th element of mydata
is missing. Now use the
mean()
function to calculate the mean of the values in
mydata
. What does R return? Confused? Next, take a look at
the help page for the function mean()
. Can you figure out
how to alter your use of the mean()
function to calculate
the mean without this missing value?
seq1
from the
workspace using the rm()
function.
End of Exercise 2