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.
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:
summary(cars)## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
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.
factorial=1
for(i in 1:12) {
factorial = factorial * i
}
print(paste("The factorial of", 12 ,"i.e. 12! =",factorial))## [1] "The factorial of 12 i.e. 12! = 479001600"
v <- as.vector(seq(from = 20, to = 50, by = 5))
print(v)## [1] 20 25 30 35 40 45 50
class(v)## [1] "numeric"
A quadratic equation is any equation having the form: ax^2+bx+c=0, provided a!=0 Solution: x=(-b+-sqrt(b^2-4ac))/2a
factorial <- function(a,b,c) {
# Check if the equation is quadratic, based on inputs
if (a == 0) {
return("Equation is not quadratic")
}
delta <- (b^2 - 4*a*c)
if (delta > 0) {
# Two real solutions
solution1 <- (-b + sqrt(delta)) / (2*a)
solution2 <- (-b - sqrt(delta)) / (2*a)
return(sprintf("Equation has two real root solutions: %s and %s", solution1, solution2))
} else if (delta == 0) {
# One real solution
solution1 <- -b / (2*a)
return(sprintf("Equation has only one root solution: %s", solution1))
} else {
# Two complex solutions
solution1 <- complex(real=-b/(2*a), imaginary=sqrt(-delta)/(2*a))
solution2 <- complex(real=-b/(2*a), imaginary=-sqrt(-delta)/(2*a))
return(sprintf("Equation has no real roots but two complex solutions: %s and %s", solution1, solution2))
}
}
factorial (1,2,1)## [1] "Equation has only one root solution: -1"