This is Mengqin Cai’s Week 1 HomeWork for R.
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. You can embed an R code chunk like this:
#1. Write a loop that calculates 12-factorial
factorial <- 1
for(i in 1:12){
factorial <-factorial*i
}
factorial
## [1] 479001600
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
seq(from = 20, to = 50, by = 5)
## [1] 20 25 30 35 40 45 50
#3. Create the function “factorial” that takes a trio of input numbers a, b, and c and solve the quadratic
#equation. The function should print as output the two solutions.
factorial<-function(a,b,c){
x1 <- (-b + sqrt(b*b-4*a*c))/(2*a)
x2 <- (-b - sqrt(b*b-4*a*c))/(2*a)
print(x1)
print(x2)
}
factorial(1,-4,3)
## [1] 3
## [1] 1
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.