MSDS Winter 2018 R Workshop

Jiadi Li

Assignment #1

1 Write a loop that calculates 12-factorial

ranNum <- 12

factorial <- 1

for (i in 1:ranNum) { factorial <- factorial* i }

factorial #prints 479001600

2 Show how to create a numeric vector that contains the sequence from 20 to 50 by 5

numVec <- c(20:50)

numVec <-numVec[numVec%%5==0]

numVec

3 Create the function “factorial” that takes a trio of input numbers a, b and c and solve the quadratic equation

factorial <- function(a,b,c){ if(bb-4ac>=0){ x1 <- (-b+sqrt(bb-4ac))/(2a) x2 <- (-b-sqrt(bb-4ac))/(2*a) if (x1==x2){ cat(“x=”,x1) } else{ cat(“x1=”,x1,“, x2=”,x2) } } else{ print(“Not applicable”) } }