Description
You should put all your code in a file called h1.R. You should print out this file and hand it in. Alternatively you could use Rmarkdown, in which case print out the html file.
1. Write code to
(a) Construct a vector with values 10,11,12,13,14. Do this using :, c and seq. Assign your result to x.
(b) Construct a vector with values 14,10,11,13,12. Call it y.
(c) Extract the third value of x.
(d) Extract the second and fourth values of y.
(e) Write a sentence explaining what the next line of code does.
sum(x>y)
(f) Write a sentence explaining what the next line of code does. (Look up the help page for abs.)
sum(abs(x-y) == 1)
(g) Write code which implements the following formula.
(h) Form a 5 × 2 matrix using the values in x and y.
2. A 2009 Eurostat report lists the percentage of the population in EU countries living with in a dwelling that (i) is too dark, (ii) has a leaking roof or is damp, (iii) has no bath or shower and (iv) has no indoor flushing toilet.
Download the data from Moodle and read in the data using
house <- read.csv(“house.csv”,row.names=1)
You might need to insert the path to the folder containing the csv file. Write R code for following.
(a) Use head to check the first few rows of the data.
(b) Extract the row of the data for Ireland.
(c) Find Spain’s value for TooDark.
(d) For how many countries is the percentage without an indoor toilet 0?
(e) For how many countries is the percentage without an indoor toilet and without a bath/shower both 0?
(f) Find the names of the countries where the percentage without an indoor toilet and without a bar/shower are both zero. Your code should give a vector of the country names.
(g) Find the maximum value of the variable Damp.
3. Download the worldcup.Rdata file from Moodle and type in
load(“worldcup.Rdata”)
You might need to insert the path to the folder containing the Rdata file.
The vector ngoals has contains the number of goals scored in every world cup final match from 1990 to 2002. Only goals in the 90 minute regulation time are considered. See next page for more information.
(a) Draw a barplot of the number of goals per match. What is the most frequent number of goals? Find the mean and the median.
(b) The first 52 matches were played in 1990, the next 52 matches were played in 1994, the next 64 matches were played in 1998, and the last 64 matches were played in 2002. Therefore elements 52, 104, 168 and 232 of ngoals give the numbers of matches scored in the four finals. Extract these elements of ngoals into a vector.
(c) Make a vector of length 232 containing the year each match was played. Call your vector year. (Hint: use rep.)
(d) Turn the vector year into a factor. Make a data frame where the first variable is ngoals and the second is year.
(e) Make a vector called matchn containing the number of the match for that world cup. The vector should be 1..52, 1..52, 1..64, 1..64. Add this vector to the data frame.
(f) Extract the rows 52, 104, 168 and 232 of the data frame. Which year’s final has the most goals?
(g) Make a boxplot of the number of goals played for each year. What can you say about the 8 goal match?
(h) (Advanced, optional). The vector gtimes contains the cumulative time in minutes from the start of the 1990 world cup in which goals were scored (counting playing time only.) Construct another vector minute containing the minute of each game in which the goal is scored. In what minutes of the matches are goals most likely to occur?
Reviews
There are no reviews yet.