—HUMAIRA RIZWAN

title: ‘CIND 123 WINTER 2017 - Assignment #1’ output: pdf_document —

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.

Question 1

  1. Use the seq() function to create the vector \((1, 5, 9, 13, \ldots, 41)\). Note that each term in this sequence is of the form \(1 + 4n\) where \(n = 1, \ldots, 10\).
seq(1,41, by=4)
##  [1]  1  5  9 13 17 21 25 29 33 37 41
  1. Use seq() and c() to create the vector \((2, 3, 4, \ldots, 10, 9, 8, \ldots, 2)\).
c(seq(2,10),seq(9,2))
##  [1]  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2
  1. Use rep() to create the vector \((1,2,3,\dots,1,2,3)\) in which the sequence \((1,2,3)\) is repeated 5 times.
rep(1:3,5)
##  [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
  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.
rep(1:3,each=7)
##  [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3
  1. Use rep() to create the vector \((10,20,20,30,30,30,\ldots,100,\ldots,100)\) where \(10n\) is repeated \(n\) times.
rep(seq (10 ,100, by=10), (1:10))
##  [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\]
sum(seq(1:100))
## [1] 5050
  1. Compute: \[\sum_{n=1}^{100} n^2\]
sum(seq(1:100)^2)
## [1] 338350
  1. Compute: \[\sum_{n=0}^{10} \frac{1}{n!}\] Hint: Use factorial(n) to compute \(n!\)
sum(1/(factorial(0:10)))
## [1] 2.718282

Question 3

  1. Create an empty list mylist.
mylist<-c()
mylist
## NULL
  1. Add a component named aa whose value is 42.
aa<-42
aa
## [1] 42
  1. Add a component named bb whose value is the numeric vector \((1,2,\ldots,10)\).
bb<-(1:10)
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”, “CKME 132”).
cc<-c("Hello","CKME 132")
cc
## [1] "Hello"    "CKME 132"
  1. Add a component named dd whose value is a 4x3 matrix whose elements are \((1,2,\ldots,12)\) in column-major order.
dd<-matrix(1:12,nrow=3,byrow = 4)
dd
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
  1. Print mylist.
mylist<-list(aa,bb,cc,dd)
print(mylist)
## [[1]]
## [1] 42
## 
## [[2]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[3]]
## [1] "Hello"    "CKME 132"
## 
## [[4]]
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   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. Print the head of the thuesen data frame.
head(thuesen)
##   blood.glucose short.velocity
## 1          15.3           1.76
## 2          10.8           1.34
## 3           8.1           1.27
## 4          19.5           1.47
## 5           7.2           1.27
## 6           5.3           1.49
  1. Compute the mean of each variable using sapply(), removing NA values.
sapply(thuesen ,mean ,rm = NA )
##  blood.glucose short.velocity 
##           10.3             NA
  1. Create three numeric vectors n1, n2, and n3 whose elements are the integers from 1 to 20, their squares, and their cubes.
n1<-c(1:20)
n1
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
n2<-c(n1^2)
n2
##  [1]   1   4   9  16  25  36  49  64  81 100 121 144 169 196 225 256 289
## [18] 324 361 400
n3<-c(n1^3)
n3
##  [1]    1    8   27   64  125  216  343  512  729 1000 1331 1728 2197 2744
## [15] 3375 4096 4913 5832 6859 8000
  1. Create a new data frame nn from the above three vectors
nn<-data.frame(n1,n2,n3)
nn
##    n1  n2   n3
## 1   1   1    1
## 2   2   4    8
## 3   3   9   27
## 4   4  16   64
## 5   5  25  125
## 6   6  36  216
## 7   7  49  343
## 8   8  64  512
## 9   9  81  729
## 10 10 100 1000
## 11 11 121 1331
## 12 12 144 1728
## 13 13 169 2197
## 14 14 196 2744
## 15 15 225 3375
## 16 16 256 4096
## 17 17 289 4913
## 18 18 324 5832
## 19 19 361 6859
## 20 20 400 8000
  1. Print the tail of nn.
tail(nn)
##    n1  n2   n3
## 15 15 225 3375
## 16 16 256 4096
## 17 17 289 4913
## 18 18 324 5832
## 19 19 361 6859
## 20 20 400 8000
  1. Compute the sum of each variable in nn using sapply.
sapply(nn ,mean)
##     n1     n2     n3 
##   10.5  143.5 2205.0

END of Assignment #1.