Introduction

In this class we will use a programming language called R. R is an open-source language and environment for statistical computing and graphics. Additionally, we will be using a third-party program called RStudio, which is an Integrated Development Environment (IDE). RStudio is a user-friendly program in which we can run R. Before completing this R tutorial, you will need to install R and RStudio on your computer: Instructions to install R and RStudio. If you are unable to install R or RStudio, see instruction in Assignment 1 for setting up RStudio Cloud.

Please complete the following questions in RStudio and save your work as an R markdown file (file extension: .Rmd). Here is a Quick introduction to R markdown and a Complete guide to R Markdown.

Instructions

Download the Assignment02.Rmd file (right click on this link, select Save Link As..) and open the it in RStudio. Then complete this assignment by filling in each answer in the R markdown document. For full credit, show your R code within the designated R Chunks and print the answers (see example answer to first question below). Run the chunks by clicking on the “Run” button in the code editor menu. You may use the internet to help you solve these problems. You should not have to use your math brain to answer the question below - use R to do the computational heavy lifting. Remember to save often.

Deliverables

When finished, click the Knit button to generate an HTML document. Click the little arrow next to the knit button and select Knit to PDF to generate a PDF document. If your computer will not generate a PDF, you can Knit to Word instead. Please upload to Canvas each of the following items (submitted as seperate files, not as a single zip file):

  1. A completed .Rmd file
  2. A PDF (or Word) file knitted from the completed .Rmd file.

Questions

  1. Compute 3/5. Set result to the variable A and print A.
A = 3/5
A
## [1] 0.6


2. Compute e^Ï€. Hint: you may need to search the internet to find the symbols for Euler’s number (e) and pi (Ï€).

exp(pi)
## [1] 23.14069


3. Make a vector C of length 10, starting with 3 and skipping every 2 integers. Print C.

c =seq(from =3,by = 3,length=10)
c
##  [1]  3  6  9 12 15 18 21 24 27 30


4. Raise each element of C by the power of 2.

c^2
##  [1]   9  36  81 144 225 324 441 576 729 900


5. Plot the square of vector C versus the index of the elements.

plot(c^2,type = "l",col="red", ylab="(C)", lwd=2, font.lab=2, main="Comparison",
     cex.axis = 1.5,cex.lab = 1.5,font=2, cex.main=1.5)


6. Sort vector C in reverse order.

E=rev(c)
E
##  [1] 30 27 24 21 18 15 12  9  6  3


7. Which elements of C are greater than 10? Set result to the variable B and print B.

c[c>10]
## [1] 12 15 18 21 24 27 30


8. Add all the elements of C together.

sum(c)
## [1] 165


9. Divide all the elements of C by the sum of C.

c/(sum(c))
##  [1] 0.01818182 0.03636364 0.05454545 0.07272727 0.09090909 0.10909091
##  [7] 0.12727273 0.14545455 0.16363636 0.18181818


10. Compute a sequence of 25 numbers from −π to π.

I=sample(-pi:pi,25,replace=T)
I
##  [1]  0.8584073  0.8584073 -3.1415927  0.8584073 -2.1415927 -3.1415927
##  [7] -0.1415927 -1.1415927 -0.1415927  2.8584073 -2.1415927  0.8584073
## [13] -3.1415927 -2.1415927 -2.1415927 -3.1415927 -0.1415927 -3.1415927
## [19] -1.1415927  0.8584073  2.8584073  2.8584073 -0.1415927  0.8584073
## [25]  2.8584073


11. Compute the first difference of the vector C (difference between each vector element).

 diff(c)
## [1] 3 3 3 3 3 3 3 3 3


12. Now compute the second difference of the vector C (the difference between every second vector element).

diff(c,2,1)
## [1] 6 6 6 6 6 6 6 6


13. Get 48 random, uniformly distributed numbers, put in vector L and print L.

L<- runif(48, min=1, max=10)
L
##  [1] 5.113303 5.440307 8.870162 4.335656 9.995051 5.561188 2.650766 3.627332
##  [9] 7.688638 7.547413 5.573179 3.627542 2.305015 4.745796 6.328993 4.561765
## [17] 2.639411 5.996338 2.761514 4.825604 2.247196 8.977514 6.110101 6.969673
## [25] 9.469193 3.198046 9.439709 6.550925 2.374848 3.230980 8.488634 1.070360
## [33] 7.332801 5.228566 3.709364 5.310959 1.406134 6.274355 5.888970 3.058645
## [41] 5.563549 5.308785 4.570773 5.945631 9.071052 4.000562 1.825149 1.587457


14. Calculate the maximum, minimum and median of L.

min(L)
## [1] 1.07036
max(L)
## [1] 9.995051
median(L)
## [1] 5.268676


15. Plot a histogram of L. This is an empirical distribution.

hist(L)


16. Concatenate the 2 vectors, C and C^2, using the ‘c’ function. For example: c(B,L).

c(c, c^2)
##  [1]   3   6   9  12  15  18  21  24  27  30   9  36  81 144 225 324 441 576 729
## [20] 900


17. Form a 6 by 8 matrix of random numbers from vector L in question 13. Set matrix to the variable M and print M.

m<- matrix(L, 6,8)
m
##          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
## [1,] 5.113303 2.650766 2.305015 2.761514 9.469193 8.488634 1.406134 4.570773
## [2,] 5.440307 3.627332 4.745796 4.825604 3.198046 1.070360 6.274355 5.945631
## [3,] 8.870162 7.688638 6.328993 2.247196 9.439709 7.332801 5.888970 9.071052
## [4,] 4.335656 7.547413 4.561765 8.977514 6.550925 5.228566 3.058645 4.000562
## [5,] 9.995051 5.573179 2.639411 6.110101 2.374848 3.709364 5.563549 1.825149
## [6,] 5.561188 3.627542 5.996338 6.969673 3.230980 5.310959 5.308785 1.587457


18. Select the 3rd row of M and set it equal to the variable P. Print P.

P=m[3,]
P
## [1] 8.870162 7.688638 6.328993 2.247196 9.439709 7.332801 5.888970 9.071052


19. Select the 5th row and set it to Q. Print Q.

Q=m[5,]
Q
## [1] 9.995051 5.573179 2.639411 6.110101 2.374848 3.709364 5.563549 1.825149


20. Compute the dot product of P and Q.

P %*% Q
##          [,1]
## [1,] 260.8808