This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

Use RStudio for this assignment. Edit the file assignment-1.Rmd and insert your R code where wherever you see the string “INSERT YOUR ANSWER HERE”

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Sample Question and Solution

Use seq() to create the vector \((1,2,3,\ldots,10)\).

seq(1,10)
##  [1]  1  2  3  4  5  6  7  8  9 10

Question 1

  1. Use the seq() function to create the vector \((1, 7, 13, \ldots, 61)\). Note that each term in this sequence is of the form \(1 + 6n\) where \(n = 0, \ldots, 10\).
seq(1,61, by=6)
##  [1]  1  7 13 19 25 31 37 43 49 55 61
  1. Use seq() and c() to create the vector \((1, 2, 3, \ldots, 10, 9, 8, \ldots, 3, 2, 1)\).
c(seq(1,10), seq(9,1))
##  [1]  1  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2  1
  1. Use rep() to create the vector \((2,3,4,\dots,2,3,4)\) in which the sequence \((2,3,4)\) is repeated 5 times.
rep(c(2,3,4),5)
##  [1] 2 3 4 2 3 4 2 3 4 2 3 4 2 3 4
  1. Use rep() to create the vector \((1,1,\ldots,1,2,2,\ldots,2,3,3,\ldots,3)\) where each number is repeated 7 times.
x <- seq(1,7)
rep(x, each=7)
##  [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5
## [36] 6 6 6 6 6 6 6 7 7 7 7 7 7 7
  1. Use rep() to create the vector \((10,20,20,30,30,30,\ldots,100,\ldots,100)\) where \(10n\) is repeated \(n\) times.
n <- seq(1,10)
rep(n*10, n)
##  [1]  10  20  20  30  30  30  40  40  40  40  50  50  50  50  50  60  60
## [18]  60  60  60  60  70  70  70  70  70  70  70  80  80  80  80  80  80
## [35]  80  80  90  90  90  90  90  90  90  90  90 100 100 100 100 100 100
## [52] 100 100 100 100

Question 2

  1. Compute: \[\sum_{n=1}^{100} n\]
n <- seq(1,100)
sum(n)
## [1] 5050
  1. Compute: \[\sum_{n=10}^{100} n^3\]
n <-seq(10,100)
sum(n^3)
## [1] 25500475
  1. Compute: \[\sum_{n=1}^{10}\left(\frac{2^{n}}{n} + \frac{4^{n}}{n^{4}}\right)\]
n <- seq(1,10)
sum(((2^n)/n)+((4^n)/(n^4)))
## [1] 416.5333
  1. Compute: \[\sum_{n=0}^{10} \frac{1}{n!}\] Hint: Use factorial(n) to compute \(n!\)
n <- seq(0,10)
sum((1/(factorial(n))))
## [1] 2.718282
  1. Compute: \[\sum_{n=1}^{20} \left(2n + \frac{1}{\sqrt{n}}\right)\]
n <- seq(1,20)
sum((2*n)+(1/sqrt(n)))
## [1] 427.5953

Question 3

  1. Create an empty list mylist.
mylist <- list()
  1. Add a component named aa whose value is 42.
mylist[["aa"]] <- 42; mylist
## $aa
## [1] 42
  1. Add a component named bb whose value is the numeric vector \((1,2,\ldots,10)\).
mylist[["bb"]] <- seq(1,10); mylist
## $aa
## [1] 42
## 
## $bb
##  [1]  1  2  3  4  5  6  7  8  9 10
  1. Add a component named cc whose value is the character vector (“Hello”, “CIND 123”).
mylist[["cc"]] <- c("Hello", "CIND 123"); mylist
## $aa
## [1] 42
## 
## $bb
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $cc
## [1] "Hello"    "CIND 123"
  1. Add a component named dd whose value is a 4x3 matrix whose elements are \((1,2,\ldots,12)\) in row-wise order.
mylist[["dd"]] <- matrix(data = 1:12, nrow = 4, ncol = 3); mylist
## $aa
## [1] 42
## 
## $bb
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $cc
## [1] "Hello"    "CIND 123"
## 
## $dd
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
  1. Display mylist on the screen.
mylist
## $aa
## [1] 42
## 
## $bb
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $cc
## [1] "Hello"    "CIND 123"
## 
## $dd
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12

Question 4

If you have not already done so, install the ISwR package on your computer using the command install.packages("ISwR").

Load the ISwR package into your session.

library(ISwR)
  1. Display the last 6 rows of the thuesen data frame.
tail(thuesen, 6)
##    blood.glucose short.velocity
## 19          12.5           1.19
## 20          16.1           1.05
## 21          13.3           1.32
## 22           4.9           1.03
## 23           8.8           1.12
## 24           9.5           1.70
  1. Compute the mean of each variable using sapply() function.

Hint: You might need to consider removing the NA values, otherwise the average will not be computed.

thuesenadj <- na.omit(thuesen)
sapply(thuesenadj, mean)
##  blood.glucose short.velocity 
##      10.373913       1.325652
  1. Create a numeric vectors v1, v2, and v3 whose elements are the numbers from 1 to 20, their squares, and their square-roots respectiverly.
v1 <- seq(1,20)
v2 <- v1 ^ 2
v3 <- sqrt(v1)
  1. Create a new data frame iData by combining the v1, v2, and v3 together in a column-wise perspective.
iData <- data.frame(v1, v2, v3)
  1. Display the first quartile of iData.
summary(iData)
##        v1              v2               v3       
##  Min.   : 1.00   Min.   :  1.00   Min.   :1.000  
##  1st Qu.: 5.75   1st Qu.: 33.25   1st Qu.:2.396  
##  Median :10.50   Median :110.50   Median :3.239  
##  Mean   :10.50   Mean   :143.50   Mean   :3.083  
##  3rd Qu.:15.25   3rd Qu.:232.75   3rd Qu.:3.905  
##  Max.   :20.00   Max.   :400.00   Max.   :4.472
  1. Create a 5x5 empty matrix, i.e. all elements equal to NA, and fill the diagonal from the top left corner to the bottom right corner of `mat1’ with the values ‘This’ ‘is’ ‘the’ ‘main’ ‘diagonal’ then display mat1.
mat1 <- matrix(data=NA, nrow = 5, ncol = 5)
mat1[1,1] <- "This"
mat1[2,2] <- "is"
mat1[3,3] <- "the"
mat1[4,4] <- "main"
mat1[5,5] <- "diagonal"
mat1
##      [,1]   [,2] [,3]  [,4]   [,5]      
## [1,] "This" NA   NA    NA     NA        
## [2,] NA     "is" NA    NA     NA        
## [3,] NA     NA   "the" NA     NA        
## [4,] NA     NA   NA    "main" NA        
## [5,] NA     NA   NA    NA     "diagonal"

END of Assignment #1.