I will list the homework at the begining of each weekly schedule, so that you have a better idea of what you need to do.
For the homework, you need to submit two files: a markdown file named homework1.md, and an R file named homework1.R.
In homework1.md, answer following questions:
In homework1.R:
Write R program to do the following. NOTE: if you haven’t
learned, the functions for computing sum and mean of vector/matrix is
sum and mean. Look at their function helps if
needed.
Compute the sum and mean of integers from 1 to 10.
Create a vector of 5 elements, you can put in some random numbers in the vector. Take out the 2nd to 4th elements, and compute their sum and mean.
Create a vector of 5 elements, you can put in some random numbers in the vector. Take out the 2nd and 4th elements, and compute their sum and mean.
Create a 3x3 matrix, you can put in some random numbers in the matrix. Compute the sum and mean of each row and column.
Create two vectors, each with 3 elements. Compute the sum and difference of each element in the vector.
Create two vectors, each with 3 elements, combine them into one vector of 6 elements.
Create two vectors, each with 3 elements, (1) combine them into a 2x3 matrix; (2) combine them into a 3x2 matrix.
Run following code to create a string:
string = "This is a string"
This by That in the string.Run following code to create a vector of strings
sample(c("car", "truck", "plane"), 100, replace=TRUE)
car by bike in the vector, by
changing the level of the factor.Initialize the vector x using the following commands. Present a one line R statement that will count the total numbers that are evenly divisible by 15 and 25. The answer will be a single number.
set.seed(222)
x = floor(rnorm(300, 200, 90))Given the following vector provide a one line command that computes the sum of all element values that are within 5 of the maximum number of this vector. The correct answer is 599.0835. You must present a one line R statement that issues this response. Hint, using the absolute function will be useful.
set.seed(123)
x = rnorm(100, 100, 10)
seq is a useful function to create a sequence of
numbers in a vector. Read the function help for seq.
Read the section for logical operators again at https://www.guru99.com/r-data-types-operator.html#5. Note, R has ability to compare a vector or matrix to a single element. In that case, each elemenet in the vector or matrix will be compared to the single element, and the return will be a logical vector/matrix.
How many numbers in 1:100 can be evenly divided by 3?
The comparison can be performed on characters too. Run following
code to create a vector of strings
sample(c("car", "truck", "plane"), 100, replace=TRUE)
How many numbers in 1:100 are greater than 20?
How many numbers in 1:100 are greater than 20 AND smaller than 50?
How many numbers in 1:100 are smaller than 20 OR or greater than 50?
Use a combination of comparison and indexing can extract uesful information from a large dataset. Here let’s do something a little interesting. I obtained the career regular season statistics for Shaquille O’Neal. You can read them into R using
shaqStat = read.csv("http://www.haowulab.org/DataSummerCamp/data/shaqStat.csv")
What you read in is a data frame, similar to a matrix.
We will learn it next week. Take a look at shaqStat in R.
Let’s do some simple practice