R Markdown

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

Loop that calcualates 12-factorial

factorial <- 1

for(i in 1:12) {
factorial = factorial * i
}
print(paste("The factorial of 12 is",factorial))
## [1] "The factorial of 12 is 479001600"

Create a numeric vector that contains the sequence from 20 to 50 by 5

x <- 20:50
y <- x[x%%5==0] #only allow 5 multiples
y
## [1] 20 25 30 35 40 45 50

Function that takes a trio of input numbers a,b,c to solve quadratic equation

a <- as.numeric( readline(prompt="Enter Input a: ") )
## Enter Input a:
b <- as.numeric(  readline(prompt="Enter Input b: ") )
## Enter Input b:
c <- as.numeric( readline(prompt="Enter Input c: ") )
## Enter Input c:
quadratic <- function(a,b,c) {
  print(sprintf("Quadratic equation:  %sx^2 + %sx + %s", a,b,c))
  
  
  discriminant <- (b^2) - (4*a*c)

  if (is.na(discriminant) ) {
     return(print(paste("The discriminant is NA.")))
  }
  else if(TRUE && (discriminant < 0)) {
    return(print(paste("This quadratic equation has no real numbered roots.")))
  }
  else if (TRUE && (discriminant > 0)) {
    x_numeric_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_numeric_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(print(paste("The two x-intercepts  are ",
                    x_numeric_plus , " and ", x_numeric_neg)))
  }
  else { #discriminant = 0  case
    x_numeric <- (-b) / (2*a)
    return(print(paste("The quadratic equation has only one root. This root is ",
                  x_numeric)))
  }
}
quadratic(a,b,c)
## [1] "Quadratic equation:  NAx^2 + NAx + NA"
## [1] "The discriminant is NA."