data(sunspots)
sunspot_mat <- matrix(as.vector(sunspots),ncol=12,byrow = TRUE)
colnames(sunspot_mat) <- as.character(1:12)
rownames(sunspot_mat) <- as.character(1749:1983)
Appendix E — Matrix Exercises
E.1 Data preparation
For this set of exercises, we are going to rely on a dataset that comes with R. It gives the number of sunspots per month from 1749-1983. The dataset comes as a ts
or time series data type which I convert to a matrix using the following code.
Just run the code as is and focus on the rest of the exercises.
E.2 Exercises
- After the conversion above, what does
sunspot_mat
look like? Use functions to find the number of rows, the number of columns, the class, and some basic summary statistics.
- Practice subsetting the matrix a bit by selecting:
- The first 10 years (rows)
- The month of July (7th column)
- The value for July, 1979 using the rowname to do the selection.
sunspot_mat[1:10,]
sunspot_mat[,7]
sunspot_mat['1979',7]
These next few exercises take advantage of the fact that calling a univariate statistical function (one that expects a vector) works for matrices by just making a vector of all the values in the matrix.
- What is the highest (max) number of sunspots recorded in these data?
max(sunspot_mat)
- And the minimum?
min(sunspot_mat)
- And the overall mean and median?
- Use the
hist()
function to look at the distribution of all the monthly sunspot data.
hist(sunspot_mat)
- Read about the
breaks
argument tohist()
to try to increase the number of breaks in the histogram to increase the resolution slightly. Adjust yourhist()
and breaks to your liking.
hist(sunspot_mat, breaks=40)
Now, let’s move on to summarizing the data a bit to learn about the pattern of sunspots varies by month or by year.
- Examine the dataset again. What do the columns represent? And the rows?
# just a quick glimpse of the data will give us a sense
head(sunspot_mat)
- We’d like to look at the distribution of sunspots by month. How can we do that?
- Assign the month summary above to a variable and summarize it to get a sense of the spread over months.
- Play the same game for years to get the per-year mean?
- Make a plot of the yearly means. Do you see a pattern?