2.9 Lab 1: Introduction to R Programming
For this lab, we will do several practice problems related to programming in R.
Create two vectors as follows
<- seq(2,10,by=2) x <- c(3,5,7,11,13) y
Add
x
andy
, subtracty
fromx
, multiplyx
andy
, and dividex
byy
and report your results.The geometric mean of a set of numbers is an alternative measure of central tendency to the more common “arithmetic mean” (this is the mean that we are used to). For a set of \(J\) numbers, \(x_1,x_2,\ldots,x_J\), the geometric mean is defined as
\[ (x_1 \cdot x_2 \cdot \cdots \cdot x_J)^{1/J} \]
Write a function called
geometric_mean
that takes in a vector of numbers and computes their geometric mean. Compute the geometric mean ofc(10,8,13)
Use the
lubridate
package to figure out how many days it has been since Jan. 1, 1981.mtcars
is one of the data frames that comes packaged with base R.How many observations does
mtcars
have?How many columns does
mtcars
have?What are the names of the columns of
mtcars
?Print only the rows of
mtcars
for cars that get at least 20 mpgPrint only the rows of
mtcars
that get at least 20 mpg and have at least 100 horsepower (it is in the column calledhp
)Print only the rows of
mtcars
that have 6 or more cylinders (it is in the column labeldcyl
) or at least 100 horsepowerRecover the 10th row of
mtcars
Sort the rows of
mtcars
by mpg (from highest to lowest)