Write a loop that calculates 13-factorial. Bonus - try to do it two different ways (for example use a for loop and a while loop). Do not use the standard factorial function. The goal is to learn about how R uses loops.
fact = 1
for (i in 1:13){
fact = fact * i
}
fact
## [1] 6227020800
i <- 1
fact1 = 1
while (i<= 13) {
fact1 = fact1 * i
i = i + 1
}
fact1
## [1] 6227020800
#check <- factorial(13)
Show how to create a numeric vector that contains the sequence from 10 to 50 by 5.
num_vector <- seq(10,50,5)
num_vector
## [1] 10 15 20 25 30 35 40 45 50
Create the function “lines” that takes two x,y points and calculates three things: the distance between those two points, the slope and the y intercept. The function should allow you to input both x and y values and give you the three answers.
x1 <- -3
y1 <- 4
x2 <- 6
y2 <- -2
lines <- function(x1,y1,x2,y2) {
distance <- (sqrt((x2 - x1)^2 +(y2-y1)^2))
slope <- (y2 - y1)/(x2 - x1)
y_intercept <- y1 - (slope*x1)
lines_data <- list(c(distance),c(slope),c(y_intercept))
names(lines_data) <- c("distance","slope","y_intercept")
lines_data
}
answer <- lines(x1,y1,x2,y2)
answer
## $distance
## [1] 10.81665
##
## $slope
## [1] -0.6666667
##
## $y_intercept
## [1] 2