Create and save the file p1.R on your computer. All your work should be in p1.R. The file p1.R should be submitted on Blackboard at the end of the precept. You must work with one partner. You should work collaboratively. Both partners are responsible for being able to explain the work that’s been done to the preceptor. In a comment at the top of the submitted file, please indicate the names of both partners.

Some of you will be tempted to use for and while-loops to solve some of the problems below (if you’ve used those before). Please don’t do this – the goal here is to try to use R the way professional data scientists use it, which usually means no loops.

Problem 1: Vectors and Variables

Define the vector 42 43 45 49 501, and store it in a variable called my.vec.

Write code to extract the second and fourth element of the vector. Explain the difference between my.vec and "my.vec"

Problem 2(a): Functions

Write a function that takes in a vector and returns a new vector that cotains the second and fourth elements of that vector. Test this function by calling it. Include the calls to the function in the file you are submitting/showing to your preceptor.

Problem 2(b): Functions and Conditionals

Write a function that outputs the solution to the quadratic equation $$ax^2 + bx + c = 0$$, given $$a$$, $$b$$, and $$c$$. Test your function when there are two, one, and no solutions.

Problem 3: Gapminder

Run the following once:

install.packages("gapminder")

In your p1.R, include library(gapminder)

Look at the data frame gapminder, and figure out what each column contains. You should be able to explain this to your preceptor. You can run ?gapminder in R to read the accompanying description to the dataset.

Problem 3(a)

Write a function that computes how many countries in the dataset there are on a given continent. Test this function by querying it with different continent names.

Problem 3(b)

Write a function that takes in a data frame like gapminder, and returns the country with the largest life expectancy on a given continent between the years y1 and y2. Test this function.

Problem 3(c)

Write a function that computes the world population in a given year. Test this function.

Problem 4 (Challenge)

Make a new dataframe which contains the increase in life expectancy per year for each country in gapminder. The increase per year is the difference between the life expectancy in the last year and the first year, divided by the number of years.