0.1 Basic R
Use the
rep()function to construct the following vector:1 1 2 2 3 3 4 4 5 5Use
rep()to construct this vector:1 2 3 4 5 1 2 3 4 5 1 2 3 4 5Create a vector of 1300 values evenly spaced between 1 and 100.
How many of these values are greater than 91? (Hint: see
sum()as a helpful functions.)Using the vector you created of 1300 values evenly spaced between 1 and 100,
Modify the elements greater than 90 to equal 9999.
View (not modify) the first 10 values in your vector.
View (not modify) the last 10 values in your vector.
0.2 Data Frames
Make a data frame with column 1:
1,2,3,4,5,6and column 2:a,b,a,b,a,bSelect only rows with value “a” in column 2 using logical vector
mtcarsis a built-in data set likeiris: Extract the 4th row of themtcarsdata.
0.3 Functions
Make a function called
my_mean()that takes a vector of numbers as input and returns the mean of the vector.Alter your
my_mean()function to take a second argument (na.rm) with default valueFALSEthat removesNAvalues ifTRUE.Add checks to your function to make sure the input data is either numeric or logical. If it is logical convert it to numeric (Hint: look at the
stopifnot()function).The diamonds data set is included in the
ggplot2package (not by default inR). It can be read into your environment with the following function.Loop over the columns of the diamonds data set and apply your mean function to all of the numeric columns (Hint: look at the
class()function).
0.4 Rmarkdown
Create a new Rmarkdown document.
Alter the template to specify the size to be a height of
6.Add a caption to your figure.
Add a chunk that includes your
my_mean()function and calculate the mean of \(100\) randomly generated numbers (seerunif()orrnorm()).Compile your document to pdf.