a <- 2
b <- 3
c <- 4
d <- a+b+c
paste("Bob is" , d , "years old." )
## [1] "Bob is 9 years old."
The syntax of an if statement is:
if (test_expression) {
statement
}
# Example - if statement
# Try several values of x and view the results
x <- 2
if (x > 0) {
print("Positive Number")
}
## [1] "Positive Number"
The syntax of if…else statement is:
if (test_expression) {
statement1
} else {
statement2
}
#Example - if...else statement
# Try several values of x and view the results
x <- 3
if(x > 0){
print("Non-negative number")
} else {
print("Negative number")
}
## [1] "Non-negative number"
The if…else ladder (if…else…if) statement allows you execute a block of code among more than 2 alternatives
The syntax of if…else statement is:
if ( test_expression1) {
statement1
} else if ( test_expression2) {
statement2
} else if ( test_expression3) {
statement3
} else {
statement4
}
# Example - if...else ladder
# Try several values of x and view the results.
x <- 15
if ( x < 5) {
print ("Small")
} else if ( x < 10) {
print ("Medium")
} else {
print ("Big")
}
## [1] "Big"
The function runif(number of values, minimum, maximum) generates a random number uniformly between minimum (default = 0) and maximum (default = 1)
The function sort(vector) will sort values (default = ascending)
# Examples - runif
# Change the parameters and view the results.
x <- runif(1)
x
## [1] 0.3457973
y <- runif(4,3,5)
y
## [1] 3.049058 3.356454 3.210333 3.708377
a <- runif(4)
a
## [1] 0.6325920 0.3021405 0.7712657 0.4810592
b <- sort(a)
b
## [1] 0.3021405 0.4810592 0.6325920 0.7712657
c <- sort(a,decreasing = TRUE)
c
## [1] 0.7712657 0.6325920 0.4810592 0.3021405
Create a conditional statement that randomly plays Rock-Paper-Scissors.
Syntax of for loop is:
for (val in sequence)
{
statement
}
# Example - A Simple Loop
# Your favorite values of the sine function
for (i in 0:12){
sin(i/6*pi)
print(round(sin(i/6*pi),digits = 4))
}
## [1] 0
## [1] 0.5
## [1] 0.866
## [1] 1
## [1] 0.866
## [1] 0.5
## [1] 0
## [1] -0.5
## [1] -0.866
## [1] -1
## [1] -0.866
## [1] -0.5
## [1] 0
# Example - Indexed Loop
# Create a Vector of Zeros
sq <- replicate(10, 0)
# Fill the vector with Square Roots
for (i in 1:10){
sq[i] <- sqrt(i)
}
sq
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
## [9] 3.000000 3.162278
Create a loop that, for the integers from 1 to 5, generates the sum of number, the square of the number and the cube of the number.
The syntax for Writing Functions is:
func_name <- function (argument) {
statement
}
# Example - Area of a Triangle
#area1 <- function(base,height) {
#.5*base*height
#}
#area1(2,4)
# Example - Rolling Dice
dice <- function(nrolls,nsides,nsize) {
replicate(nrolls,sample(c(1:nsides),nsize, replace = TRUE))
}
a <- dice(10,4,8)
a
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 3 3 3 1 3 3 1 1 3 1
## [2,] 3 2 1 1 2 4 3 2 4 3
## [3,] 1 3 1 4 3 3 2 3 4 1
## [4,] 2 4 1 3 2 4 3 3 3 1
## [5,] 2 3 4 1 4 1 1 3 1 4
## [6,] 1 4 2 4 4 1 1 2 2 1
## [7,] 1 1 2 2 1 1 3 1 4 4
## [8,] 4 4 2 1 3 3 1 2 3 2
Create a function that finds the area of a triangle given the three side lengths.
area2 <- function(s1,s2,s3) {
s <- (s1+s2+s3)/2
sqrt(s*(s-s1)*(s-s2)*(s-s3))
}
area2(1,1,1)
## [1] 0.4330127
Create a function that solves a quadratic equation. \[x^2\]
quad=function(a,b,c){
d <- b^2-4*a*c
m <- ifelse(d<0,complex(1,0,sqrt(abs(d))),sqrt(d))
c((-b+m)/(2*a),(-b-m)/(2*a))
}
q <- quad(1,0,4)
q
## [1] 0+2i 0-2i
#paste("The solutions to the equation:" , a,"x^2 +", b, "x +", c, "= 0 are", q[1], "and", q[2],".")
A return to an earlier problem.
A stick is randomly broken in two places. Determine the probability that the three pieces will form a triangle.
Your solution should be built around a simulation and should include:
1. Creating Vectors of:
(a) Trial Numbers
(b) Cut Lengths
(c) Triangle (Yes or No)
(d) Cumulative Probabilities
2. Combining the vectors into a Tibble
3. Making a Plot of (Trial Number, Cumulative Probability)
trial <- 10000
tri <- replicate(trial,0)
cut1 <- replicate(trial,0)
cut2 <- replicate(trial,0)
cut3 <- replicate(trial,0)
prob <- replicate(trial,0)
for(i in 1:trial) {
breaks <- sort(runif(2))
trial[i] <- i
cut1[i] <- breaks[1]
cut2[i] <- breaks[2]-breaks[1]
cut3[i] <- 1 - breaks[2]
tri[i] <- ifelse(cut1[i]+cut2[i]>cut3[i] & cut1[i]+cut3[i]>cut2[i] & cut2[i]+cut3[i]>cut1[i],1,0)
prob[i] <- sum(tri)/i
}
rtri <- tibble(trial,cut1,cut2,cut3,tri,prob)
#glimpse(rtri)
plot1 <- ggplot(rtri, aes(trial,prob)) + geom_point(aes(color = factor(tri))) + geom_line()
plot1
plot2 <- ggplot(rtri, aes(cut1,cut2)) + geom_point(aes(color = factor(tri)))
plot2