$35
Instruction: Submit your homework via Blackboard. If you have difficulty submitting via Blackboard, email both TAs directly. No late homework will be accepted, based on the time the email is sent out. If you are submitting via email (not recommended), put this as the subject of your email
AMS597
1. Consider the following weights 60, 72, 34, 56, 87, 80, 89, 95, 76, 28, 48, 59. Use the R script to finish the following questions
(a) Assign all these weights as vector ‘weight’.
(b) Compute the mean of ‘weight’ and of the square of the weight.
(c) What is the length of the weight?
(d) How many weights are larger than 55?
(e) Output a vector of length equal to the length of weight, in which the element takes value “Yes” if the weight is larger than 55 and smaller than 85, and “No” otherwise.
2. Using the following script, we can generate a 3×4 matrix tmp <- matrix(rnorm(12), 3, 4). Answer the following questions.
(a) Compute the sum of the first and third column.
(b) Compute the product of the first and third row.
(c) Show the dimension of the matrix.
(d) Use ‘cat’ function to output elements in the first row that are larger than 0.5.
3. How would you check whether two vectors are the same if they may contain missing (NA) values?
4. If x is a factor with n levels and y is a length n vector, what happens if you compute y[x]?
5. Write your own function myMedian.cal which takes in a vector and return the median of the vector. Do not use the built-in function median().
6. Using the following script, we will generate a toy DNA sequence mydna <- paste(sample(c(’a’,’t’,’c’,’g’),1000,replace=T),collapse=’’). Write a function that takes a string as a input, counts the number of “cg” in the input string, and replace all “cg” with “XY”. Apply your function to mydna. (Do not use any special R packages.)
7. Write a function which reads in the file from http://www.ams.sunysb.edu/~pfkuan/ Teaching/AMS597/Data/PhoneNumber.txt and output rows containing valid phone numbers from the file. Valid phone numbers take one of the following form:
###-###-####
(###) ###-####
### ### ####
###.###.#### where # is a digit between 0-9.