HW 1: Due April 151

For all homework assignments please provide the code, the R output and your thoughtful explanation of the code.


strings <- c("s", "o", "c", "i", "a", "l", "s", "c", "i", "e", "n", "c", "e")
    1. strings[1:6] gives us the first 6 characters, spelling the words "social". Find two different ways to reverse the word string of strings[1:6] , one with and one without a function. Explain in words how each of these solutions work. Hint: remember help.search(). If you get stuck always explain what you did and how you got stuck.
    2. What does strings[-1] do?. Explain in words what this does? Provide two different solutions, one using only negative numbers and one using only positive numbers to extract the character vectors "s" "c" "i" "e" "n" "c" "e" .
    3. Create a slices of strings that creates the word "coins" c("c","o", "i", "n", "s"). Explain your code.

  1. Use the following matrix to in one line of code:
    set.seed(123)
    x <- matrix(rnorm(40), 5)
    
    1. Return the entire second column.
    2. Return the element 3rd row and 4th column.
    3. Change the element in the 2nd row, 2nd column to 3.
    4. Return the diagonal elements.
    5. Create a new matrix that is the (natural) log of the matrix
    1. Create a vector, and assign to it a random sample from a normal distribution of length 1000 with mean 4 and standard deviation 2.
    2. Create a new Boolean vector indicating which sampled points are less than two.
    3. With a single command, find the number of sampled points less than two.
    4. With a single command, find the number of sampled points between 3 and 5.
    5. Calculate the mean, max, min, median, and sum of your sample.

  2. As you may recall, an identity matrix is a n x n matrix that when you multiply it by another matrix yields the same matrix. If you need a refresher, consult your math camp materials, an intro matrix algebra book or the Internet. An identity matrix has ones on all of the diagonal elements. Create an identity matrix (hint: diag() could help) that is 3 by 3. Then create two matrices composed entirely of the number 24, one which is 3 by 3 and the other which is 8 by 3. Try to matrix multiply (remember what matrix multiplication is!) your identity matrix by both matrices. Report the results and explain what happens. Then transpose the 3 by 8 matrix and try to matrix multiply it again. Give the result and explain in words how you got there.
1. Some problems adapted from Software Carpentry and early versions 2006/2008 of class