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:
# 1.write a loop that calculates 12 factorial
# assign initial value to a variable
myfactorial <- 1
# iterate 1 through 12 using for loop
for (i in 1:12) {
# in each iterate it multiplies the index with myfactorial value and assigns the # product to my factorial
myfactorial <- myfactorial * i
}
# displyas the factorial
print(myfactorial)
## [1] 479001600
You can also embed plots, for example:
```{r pressure, echo=FA
# 2. Create a numeric vector that contains the sequence from 20 to 50 by 5.
# using seq function
num_vec <- seq(20,50, by = 5)
# displya the output
print(num_vec)
## [1] 20 25 30 35 40 45 50
# 3. Crate the function "quad" that takes trio of input numbers, a,b,and c and solve # the quadratic equation. The function should print two solutions as output.
quad <- function(a, b, c){
# calculate the discreminant and assign it to the variable discriminant
discriminant <- b^2 - 4* a* c
# now solve the discriminant over the square root and divide by 2* a
solution_a <- (-b + sqrt(discriminant))/(2* a)
solution_b <- (-b - sqrt(discriminant))/(2* a)
cat("solution a is: ", solution_a, "\n")
cat("solution b is: ", solution_b, "\n")
}
quad(1,2,1)
## solution a is: -1
## solution b is: -1
quad(1,6,5)
## solution a is: -1
## solution b is: -5
# it will become negative discriminant which will produce complex number
quad(1, 1, 1)
## Warning in sqrt(discriminant): NaNs produced
## Warning in sqrt(discriminant): NaNs produced
## solution a is: NaN
## solution b is: NaN
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.