- Suppose you have a dataset with the values 2, 136, 95, 7, and 13.
- Input the data values into a vector named
dat
.
- Find the mean, variance and standard deviation of the dataset. Save them as objects named
v
, y
, and z
, respectively. (Hint: var()
is the R command for variance).
- Create a vector named
w.sq
containing the squares of the values in this dataset.
- Use R to calculate the sum of the squares of the values in the dataset. Save the value in as
sum.w.sq
.
- Use the computational formula for the variance shown below to compute the variance of the dataset. \[s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i^2 - n \bar{x}^2)\] where n is the number of values in your dataset and \(x_1 = 2\), \(x_2 = 136\), etc. for these data. Save your result as
w.var.comp
. You should get the same answer as in part (b). Note: If you are new to summation notation, \(\sum_{i=1}^5 x_i = 2 + 136 + 95+7+13\) for these data.
- Multiply each value in the dataset by 5, then find the mean and standard deviation of the new set of values (store them in scalars named
mean.multiply.data
and sd.multiply.data
. How do they relate to the mean and standard deviation in part (b)? Multiply each value in the dataset by a number other than 5. Conjecture about the effect on the mean and standard deviation of multiplying each data value by the same fixed number.
- How do the mean and standard deviation change if we add 5 to each data value (store answer in
mean.add.5
and sd.add.5
)? Generalization of the effect on the mean and standard deviation of adding the same value to each number in the dataset. Try some calculations in R to test your conjecture.
- Create a matrix named
matrix1
with row 1 containing the values 1,8,3; row two containing the values 4,7,9; and row 3 containing the values 3, 2, 9.
- Extract the third column of the matrix and store it in a vector named
colm3
.
- Extract the element in the 3rd row and 2nd column and store in a scalar called
myelt
.
- Type
2:5
in the R console to see what it gives you. Turn that vector into a matrix by typing matrix2 <- matrix(2:5,nrow=2,ncol=2)
- What does
t(matrix2)
do to the original matrix? How about det(matrix2)
? (You can use the help files to learn about these commands.)
- Create a list named mylist with two components: 1) a 2x2 numeric matrix of your choice and 2) a character vector of names: Ruby, Miguel, Tiffany, Tyler. Name the matrix component
mymatrix
and the vector component mynames
.
- Extract the matrix from the list and find its determinant. Store the determinant as
mydet
.
- Extract the name “Tiffany” from this list and store as
favname
.
- The
swiss
dataset built into R is a dataframe with 47 rows and 6 columns. Type ?swiss
to see a description of the data in the file. You can work with this dataset as if it is already in your R Environment.
- Extract the column named Agriculture (which represents the percent of males who work in agriculture) from this dataframe. Store it as
ag.col
.
- Calculate the mean of percent of males who work in agriculture and store it as
mean.perc.male.ag
.
- Extract the 40th row and store as
row40
.
- Try the command
summary(swiss)
.