Title image Spring 2022

Creating and Displaying Matrices in R

The purpose of this project is to introduce you to the Matrix class and the image graphing function.


Project Tasks

    Part 1: Setup

    Open R Studio or a text editor and Create a new R Script or R Markdown file (your choice) called project6. Save your file to an appropriate working directory.

    Part 2: Explore creating a matrix

    Create a couple of matrix variables, fill them with data, and print them out.

  1. Create a function matrixIntro, which should contain the following code. The function should two parameters: R and C.
    1. Add a comment to the beginning of the function indicating its purpose.
          # Generate RxC matrices and print them.
      
    2. Assign to a variable data1 the result of calling the concatenate, c(), function with 6 values that are a mix of numbers, strings, and logical values.
    3. Assign to a variable m1 the result of calling the matrix() function with the data argument being data1, the ncol argument being 3, and the nrow argument being 2. Then have your code print the matrix m1. Note the type of the data in the matrix. Have your code print the typeof and class of m1.
    4. Assign to a variable data2 the result of calling c() with 6 values that are a mix of numbers and logical values. Then assign to m2 a matrix created identically to the prior step, but using data2 instead of data1. Print the matrix. Note the type of m2, and print the typeof and class of m2.
    5. Assign to a variable data3 the range 1:(R*C), where R and C are the parameters of the matrixIntro function (why do you think the parentheses are necessary in this expression?). Use data3 to create a new matrix with ncol = C and nrow = R and assign it to m3. Print m3.
    6. Using data3, assign to m4 a new matrix with arguments identical to the prior step, but add the argument byrow = TRUE. Print the matrix and note the differences with m3.
    7. After terminating your function, call matrixIntro twice: once with the parameters (3, 4) and once with the parameters (2, 5).
  2. Part 3: Explore plotting a matrix

    Create a matrix and then use the image function to plot it.

  3. Create a function matrixPlotting, which should contain the following code. The function should take in two parameters: R and C.
    1. Add a comment to the beginning of the function indicating its purpose.
      Generate an RxC matrix with random values and plot it.
      
    2. Assign to values the range 1:(R*C). Assign to mat a matrix created using values as the data with ncol = C, nrow = R, and byrow=TRUE. Print the matrix and note the order of the values.
    3. Call the image() function with the arguments: x=1:R, y=1:C, and z=mat. At this point, terminate your matrixPlotting function and then call it with the parameters (3, 4). Compare your matrix values and the resulting plot. If you are using an older version of the graphics library (e.g. 3.5.1), then your image should will (by default) use a color scheme (heatmap) in which red is the lowest value and white is the highest value. If you are using a newer version (e.g. 3.6.2), the default color scheme is in the opposite direction (yellow is lowest value and red is highest value). You can tell which default color scheme is used by looking up "image" in the Help window. If it says col = hcl.colors( 12, "YlOrRd", rev = True), then you are using the newer scheme. If it says col = heat.color(12), then you are using the older version. Note how the matrix data is translated to an image (by row and drawn from bottom to top).
    4. In your matrixPlotting function, print the result of t(mat). Then call the image() function with the arguments: x=1:C, y=1:R, and z=t(mat). The t() function transposes the matrix (flips columns and rows). Note the differences in the two plots. The plots are transposes of one another around the diagonal axis from the lower left of the plot to the upper right.
    5. For the final step in matrixPlotting, copy the last image() statement and paste it at the end. Then add the arguments xaxt='n', yaxt='n' to turn off the automatic axis generation. Then call axis() with the arguments: side=1, at=1:C, and labels=1:C. Call axis() a second time with side=2 and using 1:R for the at and labels arguments. Running matrixPlotting again, note the differences in the plots. The axis function gives you more control over the labels.
  4. Part 4: Use a function to generate matrix data

    Write a function that generates a value given two parameters. Use the function to fill out a matrix using nested for loops, then plot the matrix. Experiment with how image plotting displays the matrix data.

  5. Create a function f() with two parameters: x and y. The function should return x*y + 2*y.
  6. Create a function matrixFunction, which should contain the following code. The function should take in three parameters: dx, dy, and step. The purpose of the function will be to fill out a matrix with the values of the function f() for the ranges x in [0, dx], y in [0, dy], stepping by step along each axis.
    1. Add a comment to the beginning of the function to indicate its purpose and the meanings of the parameters.
          # Return a matrix m such that m[i,j] is f(xs[j],ys[i])
          # where xs[j] is the jth element of a sequence beginning at 0, ending at dx, with a step of "step"
          # and ys[i] is the ith element of a sequence beginning at 0, ending at dy, with a step of "step"
      
    2. Assign to xseq the result of calling the seq() function with 0, dx, and step as arguments.
    3. Assign to yseq the result of calling the seq() function with 0, dy, and step as the arguments.
    4. Assign to mat a matrix with the number of columns being the length of xseq and the number of rows being the length of yseq.
    5. Begin a for loop with the loop variable i over the range from 1 to the length of yseq.
    6. Inside the first for loop, begin a second for loop with loop variable j over the range from 1 to the length of xseq. This configuration of for loops is called a nested for loop, and it is a common programming structure when working with matrices.
    7. Inside the inner for loop, assign to mat[i,j] the result of calling the f() function with the arguments xseq[j] and yseq[i]. Close the for loops. The nested for loop code should assign a value to each element of the matrix. Print the matrix, close the matrixFunction function, and then call matrixFunction() with the arguments 5, 10, 0.5. Note the values in the matrix.
    8. At the end of the matrixFunction function call the image() function with the arguments: y=xseq, x=yseq, and z=mat. Note the odd ordering because the image function draws rows left to right and columns bottom to top.
    9. Call image() a second time, reversing the x and y arguments and using z=t(mat). Note the differences between the two plots. In the second case, the plot follows a typical Euclidean plot, given how we calculated the values.

Report

Answer the following questions. Submit your answers as a plain text file or PDF in your handin directory. Put your name at the top of the file. No credit will be given for any other format or for a file without a name.


Turn In