COSC 2836 – Winter 2018, Assignment 4 (R)
This assignment is made up of several files. Make sure that you submit your files to the online submission system (http://d2l.laurentian.ca) as you did with the previous assignments. Make sure you put your name and student number at the top of EACH file as comments and, if necessary, clearly mark where each new question or part (i.e., a, b, etc.) begins (again, using comments). The easier it is for the markers to find your answers the happier they will be marking your assignment.
You may work in partners if you like as long as you follow these instructions for submission. Only ONE of the partners is to submit the files on D2L (in the dropbox). BOTH partner’s names must be on each file at the top in comments. The partner who submitted the assignment to the dropbox will receive feedback in the dropbox once it is graded and the other partner will simply receive the numeric grade under grades on D2L.
Read this part carefully:
This assignment is made up of 10 questions, each is worth 10 marks.
The assignment is out of 70 so submit only your best 7 responses.
At least 2 of your submitted solutions must come from questions 7 through 10. We will not grade more than 7 submitted solutions.
Do all 10 questions to prepare for the upcoming test.
1. Solve the following mathematical expressions. Save the command history required for this question as question1.txt. Be sure to put a brief comment between sections (i.e., #Part a) so that I can tell where one ends and the next begins. Tackle these one bit at a time and use variables where to break the problem down into smaller, more digestible, pieces. Be sure to store your final answer in a variable as well. Feel free to post your final answer (just the number) to D2L to compare what others are getting.
a) x3 −6×2 +11x–6forx=21.915
b) log2(100) + ln(100) + log10(100)
c)1+ + + + + forx=2 ! ! ! ! !
d)𝑒2𝑐𝑜𝑠𝛼 +𝑠𝑖𝑛𝛽𝑤h𝑒𝑟𝑒𝛼=,𝛽=√𝜋
2. Complete the following tasks. Save the commands in a history file called question2.txt. Complete the following tasks:
- Use the “:” operator to create a vector called v1 that stores values ranging from -5 to 10.
- Use the seq() function to create a vector called v2 that stores 15 values ranging from π to 2π.
- Use the seq() function to create a vector called v3 that contains values from 5 to -1 in increments of 0.35.
- Combine the three vectors into two separate matrices: a row-wise matrix called matrix1 and column-wise matrix called matrix2. Any vectors that need to be resized should be “padded” to avoid repeating values.
- Save both matrices to separate text files. For matrix1 use commas to delimit the data values and for matrix2 use tabs (you might have to look this one up).
3. Complete the following tasks. Save the commands in a history file called question3.txt.
- There is a matrix and a vector in the binary file objects.Rdata (found on D2L).
- Load these objects from the file. Determine the names of these objects and
display their contents.
- With the vector object:
o Create a new vector object called sorted1 that contains the same values but in descending order.
o Create a new vector object called sorted2 that contains the same values but in descending order.
o Create separate variables that contain the maximum, the minimum, the mean and the sum of the original vector.
o Create a new vector object called subVector1 that contains the first five values of the original vector.
o Create a new vector object called subVector2 that contains the indices (positions) of all the values in the original vector that are less than 0. (from the notes)
o Create a new vector object called subVector3 that contains all the values in the original vector that are less than 0.
With the matrix object:
o Create separate variables that contain the maximum, the minimum,
the mean and the sum of the original vector.
o Using row and column names (instead of indices) determine the mean
of the middle row and the mean of the middle column
o Create a new vector object called subV1 that contains all of the values
the first two rows of the matrix.
o Create a new matrix object called subMatrix1 that contains the
“9am” and “9pm” data in separate rows. Don’t worry about the column and row headers in the matrix. You can use multiple commands to get this done but it will be more impressive if you can do it in one!
4. Complete the following tasks. Save the commands in a history file called question4.txt.
a) Create three square matrices with random numbers between 0 and 1000. The first matrix should have 100 (10×10) values, the second, 10000 (100×100) and the third 1000000 (1000×1000). Determine the mean of each of the matrices and in a new vector store the difference between the mean you computed and the theoretical mean of 500 for each matrix. Finally, determine the mean difference of values stored in the results vector.
b) All of the random numbers in part a) are generated from a uniform distribution. Let’s try a different one. Read up on the rnorm function. Repeat the tasks from part a) but generate the values (100, 10000 and 1000000 of them) using a normal distribution with a mean of 500 and a standard deviation of 10.
5. Complete the following tasks. Save the commands in a history file called question5.txt.
Create a data frame with data of your choosing (don’t use data from the notes): it must have mixed data types. Min of 10 rows, min of 5 cols
show how to use the $ shortcut to access individual columns of data
convert your data frame to a matrix, store it as a different object (i.e.,
different name)
save both (frame and matrix) to different files, save the matrix to a text file
and the frame to a binary file.
6. Load the Beginning.RData file and perform the following tasks. Save the
commands in a history file called question6.txt.
- Determine what type of object mf is and display a summary of its structure.
- Determine the mean, median and maximum of each column in mf.
- Convert mf to a matrix and store it as a new object. Determine the sum of
each row, the mean of each row and the product of each row.
- Finally, determine the mean, standard deviation and range of the entire
data set. Don’t worry that we are crossing columns so this result is contextually meaningless.
7. Load the Beginning.RData file and perform the following tasks. Save the commands in a history file called question7.txt.
- Object invert is a data frame. Using cross tabulation create a contingency table that shows the relationship between Taxa and Habitat. Save the resulting table as a new object.
- Use the data from the table to create the following stacked barplot. Hints: The actual plot is easier than you might think. Pay attention to the details. Full marks for making the legend appear and for putting it in a better spot as shown below (I used args.legend):
Location of Insects
Ant Spider Beetle Bug Aphid
Upper Lower Stem Habitat
Individuals Observed
0 100 200 300 400
8. Use rnorm() from earlier in this assignment to create a normal distribution consisting of 10 samples with an approximated mean of 75 and standard deviation of 10.5. Plot the histogram and density line of this sample. Repeat with a second data set containing 1,000,000 samples. Your (separate) graphs should look similar to the following. Pay attention to the details. Save the commands in a history file called question8.txt.
Estimated Class Grades
60 65 70 75 80 85 90 95 Grade
Theoretical Class Grade Distribution
40 60 80 100 120 Grade
Full marks if you can get rid of the grades that are > 100 or < 0 before plotting.
Density Density
0.00 0.01 0.02 0.03 0.00 0.01 0.02 0.03 0.04 0.05 0.06
9. Using data of your own choosing – either invented, calculated or borrowed from another source, create a time series object and use it to create a line graph. Put a brief comment above these commands to explain how your data was derived and make sure you include it (file) or the commands you used to create it so I can reproduce it. Your line graph should have the following properties (including proper titles) but will have different data. Save the commands in a history file called question9.txt.
Main Title
Y Title
-2 -1 0 1 2
1962 1964
1966 1968
X Title
10. Consider the NormTemp.xls file in the D2L dropbox. Your job is to create a single barplot that has the following data:
- Mean body temperature and heart rate for males, females and combined
- Median body temperature and heart rate for males, females and
combined
Your graph must be well labeled with titles and appropriate labels below each bar. Extra consideration for creativity of the graph presentation (colours, line types etc.). Also, extra consideration will be given for minimizing the number of commands and objects required to get this job done. Show all commands required to complete this task. If commands cannot be shown for some reason, use comments to explain.