*To submit this assignment, upload the full document on blackboard, including the original questions, your code, and the output. Submit both your .Rmd source file and a knitted .html version of the same file.*

Variable assignment (1 mark)

Assign the value

`5`

to the variable/object`a`

. Display`a`

. (0.25 marks)Assign the result of

`10/3`

to the variable`b`

. Display`b`

. (0.25 marks)Assign the product of

`a`

and`b`

to`c`

. Display`c`

. (0.25 marks)Write a function that adds two numbers and returns their sum. Use it to assign the sum of

`a`

and`b`

to`c`

. Display`c`

. (In practice, there is already a more sophisticated built-in function for this:`c <- sum(a, b)`

) (0.25 marks)

Vectors (1 mark)

Create a vector

`v`

with all integers 0-30, and a vector`w`

with every third integer in the same range. (0.25 marks)What is the difference in lengths of the vectors

`v`

and`w`

? (0.25 marks)Create a new vector,

`v_square`

, with the square of elements 3, 6, 7, 10, 15, 22, 23, 24, and 30 from the variable`v`

.*Hint: Use indexing rather than a for loop.*(0.25 marks)Calculate the mean and median of the first five values from

`v_square`

. (0.25 marks)

Boolean indexing (1 mark)

Create a boolean vector

`v_bool`

, indicating which vector`v`

elements are bigger than 20. How many values are over 20?*Hint: In R, TRUE = 1, and FALSE = 0, so you can use simple arithmetic to find this out.*(0.5 marks)Use the variable

`v_bool`

as an index to extract the elements from`v`

that are bigger than 20. What are the min and max values of this new vector? (0.5 marks)

Data frames (2 marks)

There are many built-in data frames in R, which you can find more details about online. What are the column names of the built-in dataframe

`beaver1`

? How many observations (rows) and variables (columns) are there? (0.5 marks)How can you view the first 5 rows of this data frame? How can you view the last 5 rows? (0.5 marks)

What is the min, mean, and max body temperatue in this data set?

*Hint: Remember that each column in a data frame is a vector, so you can use the same functions as in the previous question on vectors.*(0.5 marks)Use the

`summary`

function to display an overview of the`temp`

column. (0.5 marks)

Data frames with dplyr (3 marks)

Use dplyr to calculate the mean temperature in the dataset. (0.25 marks)

Calculate the mean temperature for day 346. (0.5 marks)

Rather than using

`filter()`

to calculate the mean for each day separately, the more convenient`group_by()`

can be used to aggregate measurements by a categorical value (such as the`day`

column in`beaver`

). Use this approach to calculate the mean temperature and activity level for each of the days in the dataset. (0.5 marks)Express in writing what the average activity level from the above calculation means.

*Hint: Remember that you can read a description of the columns online.*(0.25 marks)How many observations are there per day in this dataset? (0.25 marks)

How many observations are there per day when the beaver is active outside the retreat? (0.25 marks)

Was the body temperature higher when the beaver was active or inactive? Is this what you would expect? (0.25 marks)

Grouping by activity level

*and*the day of the observation. Which variable seems to be more related to high body temperature: activity level or day of measurment? (0.25 marks)

This work is licensed under a Creative Commons Attribution 4.0 International License. See the licensing page for more details about copyright information.