R Question 1: for/while loop
#Answer of Q1 for loop
n<-1
for (x in 1:13) #from 1 to 13
{
n<-n*x
}
print(n)
## [1] 6227020800
#Answer of Q1 while loop
f <-1
i <-1
while (i<=12)
{
i<-i+1
f <-f*i
}
print (f)
## [1] 6227020800
R Question 2: numberic vector that contains the sequence from 10 to 50 by 5
#range from 5*2=10 and 5*10=50
x <-2:10
y <-5
x*y
## [1] 10 15 20 25 30 35 40 45 50
R Question 3: distance, slope and the y intercept of 2 points
slope <- function(xi,yi) (yi)/(xi)
distance <-function(xi,yi) sqrt(sum(((xi^2)+(yi^2))))
yint <-function(xi,yi) y1-((yi)/(xi))*x1
x1 <--2
x2 <-4
y1 <-9
y2 <-7
yi <-y2-y1
xi <-x2-x1
lines <- function(xi,yi) list(slope (xi,yi),distance (xi,yi),yint (xi,yi))
lines (xi,yi)
## [[1]]
## [1] -0.3333333
##
## [[2]]
## [1] 6.324555
##
## [[3]]
## [1] 8.333333
#for checking purpose bt using exsiting function
print ("number below is for checking purpose")
## [1] "number below is for checking purpose"
twopoint.data <- data.frame(
x=c(-2,4),
y=c(9,7)
)
dist(twopoint.data, method = 'euclidean')
## 1
## 2 6.324555
lm(y ~ x, data=twopoint.data)
##
## Call:
## lm(formula = y ~ x, data = twopoint.data)
##
## Coefficients:
## (Intercept) x
## 8.3333 -0.3333
## [1] "Thanks!"