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 can be found on Canvas (under Modules). If you are unable to install R or RStudio, see instruction for setting up RStudio Cloud on Canvas.

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

Open the assignment2.rmd file 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 (π).

A = exp(pi)
print(A)
## [1] 23.14069


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

C <- seq(from = 3, by = 2, length.out = 10)
print(C)
##  [1]  3  5  7  9 11 13 15 17 19 21


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

C <- C^2
C
##  [1]   9  25  49  81 121 169 225 289 361 441


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

plot(C^2,
     xlab = "Index",
     ylab = "C squared",
     main = "Square of Vector vs Index")


6. Sort vector C in reverse order.

C_rev <- sort(C, decreasing = TRUE)
C_rev
##  [1] 441 361 289 225 169 121  81  49  25   9


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

B <- C[C > 10]
B
## [1]  25  49  81 121 169 225 289 361 441


8. Add all the elements of C together.

total <- sum(C)
total
## [1] 1770


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

C_div <- C / sum(C)
print(C_div)
##  [1] 0.005084746 0.014124294 0.027683616 0.045762712 0.068361582 0.095480226
##  [7] 0.127118644 0.163276836 0.203954802 0.249152542


10. Take a random sample of C with a size of 5 elements.

sample_C <- sample(C, size = 5)
print(sample_C)
## [1] 361   9 289 169  81


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

 C_diff <- diff(C)
print(C_diff)
## [1] 16 24 32 40 48 56 64 72 80


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

C_second_diff <- diff(C, differences = 2)
print(C_second_diff)
## [1] 8 8 8 8 8 8 8 8


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

L <- runif(48)
print(L)
##  [1] 0.31055996 0.55411113 0.40396897 0.69334852 0.30055569 0.49828676
##  [7] 0.33599338 0.79911468 0.16177659 0.81495048 0.03319204 0.02030150
## [13] 0.41393098 0.54352196 0.47656245 0.08860522 0.60843898 0.37647255
## [19] 0.98689087 0.77469217 0.49837969 0.72883863 0.25723648 0.49666338
## [25] 0.65400218 0.83750716 0.56465267 0.93730066 0.38009488 0.47264903
## [31] 0.27452057 0.07922589 0.31113887 0.92183365 0.75773008 0.27420027
## [37] 0.66374604 0.05784104 0.64916872 0.88196303 0.03929844 0.18446709
## [43] 0.97764192 0.10828995 0.16839539 0.14734798 0.43428351 0.23149790


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

max_L <- max(L)
min_L <- min(L)
median_L <- median(L)

print(max_L)
## [1] 0.9868909
print(min_L)
## [1] 0.0203015
print(median_L)
## [1] 0.4534663


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

hist(L,
     main = "Histogram of L (Empirical Distribution)",
     xlab = "Values of L",
     ylab = "Frequency")


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

C_concat <- c(C, C^2)
print(C_concat)
##  [1]      9     25     49     81    121    169    225    289    361    441
## [11]     81    625   2401   6561  14641  28561  50625  83521 130321 194481


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, nrow = 6, ncol = 8)
print(M)
##           [,1]       [,2]       [,3]      [,4]      [,5]       [,6]       [,7]
## [1,] 0.3105600 0.33599338 0.41393098 0.9868909 0.6540022 0.27452057 0.66374604
## [2,] 0.5541111 0.79911468 0.54352196 0.7746922 0.8375072 0.07922589 0.05784104
## [3,] 0.4039690 0.16177659 0.47656245 0.4983797 0.5646527 0.31113887 0.64916872
## [4,] 0.6933485 0.81495048 0.08860522 0.7288386 0.9373007 0.92183365 0.88196303
## [5,] 0.3005557 0.03319204 0.60843898 0.2572365 0.3800949 0.75773008 0.03929844
## [6,] 0.4982868 0.02030150 0.37647255 0.4966634 0.4726490 0.27420027 0.18446709
##           [,8]
## [1,] 0.9776419
## [2,] 0.1082899
## [3,] 0.1683954
## [4,] 0.1473480
## [5,] 0.4342835
## [6,] 0.2314979


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

P <- M[3, ]
print(P)
## [1] 0.4039690 0.1617766 0.4765625 0.4983797 0.5646527 0.3111389 0.6491687
## [8] 0.1683954


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

Q <- M[5, ]
print(Q)
## [1] 0.30055569 0.03319204 0.60843898 0.25723648 0.38009488 0.75773008 0.03929844
## [8] 0.43428351


20. Compute the dot product of P and Q.

dot_product <- sum(P * Q)
print(dot_product)
## [1] 1.093969