Steven Holland

# Problem Set 1: Using R

Use the seq() command to generate a vector of numbers from 0 to 220, in steps of 2, and assign it to an object, with a name of your choosing.

In Excel, generate the same vector of numbers, export it as a CSV file, with a name in the form of XXXXVector.csv, where XXXX is your last name, lower case (e.g., mine would be hollandVector.csv). Import this file into R, assigning it to another object with a name of your choosing.

Subtract the first vector from the second, assigning the results to a third vector with a name of your choice. The result of this should produce a vector with the first elements subtracted from one another, the second elements subtracted from one another, and so on. Since the first two vectors should be identical, the third vector should be all zeroes.

Use range() on this third vector to test whether all the values are zero. If they are not, fix your work. In general, this is always true: if your work isn’t correct, fix it.

Using either your first or second vector, assign the vector to another vector named fahrenheit.

In one line of code, generate a vector called celsius that shows the equivalent Celsius temperature for each of these Fahrenheit values. Calculate these values by doing your own arithmetic conversion; don’t use any built-in conversion tool you might find for R.

In one line of code, use the which() function to find the row number (that is, the index) of the fahrenheit vector which corresponds to 32 degrees, then use this to show the value in celsius corresponding to that row number (index). Hint: learn to use which() to find the row number you need, then use it as an index on celsius. In a comment, state whether this value is correct.

Do the same (code and comment), but for a Fahrenheit temperature of 212.

Enter the following matrix directly into R (not via Excel) and name it myMatrix.

4.1 3.2 7.5 9.8
7.4 2.0 8.1 1.3
9.5 3.4 0.0 8.0
1.2 7.1 2.3 2.5
6.4 4.2 8.6 8.8
2.7 3.9 2.0 7.1
1.4 4.4 5.4 3.3

Display myMatrix to verify that all values are correct. Note that entering your data twice and comparing the results, although laborious, is almost assured of finding errors in data entry. It is important to find these early, because data errors can cause you to waste many hours on wrong analyses.

Use the colnames() function to assign column names to myMatrix, from left to right: alpha, beta, gamma, delta. Use the help() function if you are uncertain how to use colnames(); the examples on these help pages are often particularly instructive. Likewise, use the rownames() function to label the rows of myMatrix from A to G. Display myMatrix to verify that these row and column names were successfully applied.

Now enter the same matrix into Excel, with the column labels and row labels as above. Save this file as a CSV file, with a name in the form of XXXXMatrix.csv, where XXXX is your last name, lower case (e.g., mine would be hollandMatrix.csv). Import this file into R, naming it myDataFrame. Note that Excel sometimes adds extra rows causing you to get an error about duplicate row names. If this happens, fix the CSV file in a text editor and import it again.

Display myDataFrame.

Verify using the appropriate command that myMatrix is a matrix and that myDataFrame is a data frame.

Subtract myMatrix from myDataFrame and assign the result to a matrix called difference.

Use the appropriate function to determine whether difference is a matrix or data frame.

Use the range() function on difference to verify that all values equal zero.