Creating and Displaying Matrices in R
The purpose of this project is to introduce you to the Matrix class and the image graphing function.
Create a function task1, which should contain the following code. The function should two parameters: R and C.
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.
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.
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.
- Assign to a variable data3 the range
1:(R*C), where R and C are the parameters of the task1 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.
- 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.
- After terminating your function, call task1 twice: once with the parameters (3, 4) and once with the parameters (2, 5).
- Assign to a variable data1 the result of calling the concatenate,
Create a function task2, which should contain the following code. The function should take in two parameters: R and C.
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.
image()function with the arguments: x=1:R, y=1:C, and z=mat. At this point, terminate your task2 function and then call it with the parameters (3, 4). Compare your matrix values and the resulting plot. In the default color scheme (heatmap) red is the lowest value and white is the highest value. Note how the matrix data is translated to an image (by row and drawn from bottom to top).
In your task2 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.
For the final step in task2, 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 task2 again, note the differences in the plots. The axis function gives you more control over the labels.
- Assign to values the range
- Create a function f() with two parameters: x and y. The function should return x*y + 2*y.
Create a function task3, 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.
- Assign to xseq the result of calling the seq() function with 0, dx, and step as arguments.
- Assign to yseq the result of calling the seq() function with 0, dy, and step as the arguments.
- 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.
- Begin a for loop with the loop variable i over the range from 1 to the length of yseq.
- 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.
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 task3 function, and then call task3() with the arguments 5, 10, 0.5. Note the values in the matrix.
- At the end of the task3 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.
- 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.
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.
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.
- What is a Matrix?
- What kind of information is appropriate for a matrix?
- Can a matrix hold more than one type of data?
- In what order does the image function draw the content of the matrix?
Create a project6 folder inside the Private folder in your Courses directory. Put your R Script file, and your text/PDF file with your answers into the project6 folder. If you used R Markdown for the project, put your answers and the code all in the same file.