Practical 1
C029/404852/2024 Dorothy Mitchel
2025-11-08, 2025-11-08 23:58:03.675355
Question 1: Find the distance between two points (2,5) and (7,8)
# Assign the co-ordinates to variables
x1 <- 2
y1 <- 5
x2 <- 7
y2 <- 8
# Use the Euclidean Distance Formula
d = sqrt( (x2-x1)^2 + (y2-y1)^2 )
# Calculate the difference in x and y
delta_x <- x2 - x1
delta_y <- y2 - y1
# Calculate the distance
distance <- sqrt(delta_x^2 + delta_y^2)
# Print the result
print(paste("The distance between the two points is:", distance))
## [1] "The distance between the two points is: 5.8309518948453"
Question 2: Compute the expression 5 + 4( x−2x), where x = 1
# Define the value of x
x <- 1
# Calculate the expression
expression <- 5 + 4 * ( (x - 2) / x )
# Print the result
print(paste("The value for the expression when x = 1 is:", expression))
## [1] "The value for the expression when x = 1 is: 1"
1
Question 3: If x < −7.5 and y < −3 then
(a) Perform some basic arithmetic calculation on x and y
# Assign Variables which satisfies the conditions
x <- -8
y <- -5
Performing basic arithmetic operations
Addition
# Perform the equation
add_res <- x + y
# Print the result
print(paste("Addtion result is", add_res))
## [1] "Addtion result is -13"
Subtraction
# Perform the equation
sub_res <- x - y
# Print the result
print(paste("Subtraction result is", sub_res))
## [1] "Subtraction result is -3"
Multiplication
# Perform the equation
mul_res <- x * y
# Print the result
print(paste("Multiplication result is", mul_res))
## [1] "Multiplication result is 40"
Division
2
# Perform the equation
div_res <- x / y
# Print the result
print(paste("Dvision result is", div_res))
## [1] "Dvision result is 1.6"
(b) Make use of relational operators, for example, x > y, x <= y
# Assign the variable "logic"
logic <- x <= y
# Print "logic"
print(paste("Is x less than or equal to y:", logic))
## [1] "Is x less than or equal to y: TRUE"
# Assign the variable "logic"
logic <- x < y
# Print "logic"
print(paste("Is x less than y:", logic))
## [1] "Is x less than y: TRUE"
# Assign the variable "logic"
logic <- x >= y
# Print "logic"
print(paste("Is x greater than or equal to y:", logic))
## [1] "Is x greater than or equal to y: FALSE"
# Assign the variable "logic"
logic <- x == y
# Print "logic"
print(paste("Is x equal to y:", logic))
## [1] "Is x equal to y: FALSE"
(c) Compute square root, log, and exponential of x
Square root of x
3
# This will produce NaN (Not a Number) because x is negative.
sqrt_x <- sqrt(x)
## Warning in sqrt(x): NaNs produced
print(paste("Square root of x:", sqrt_x))
## [1] "Square root of x: NaN"
Natural log of x
# This will also produce NaN because x is negative.
log_x <- log(x)
## Warning in log(x): NaNs produced
print(paste("Natural log of x:", log_x))
## [1] "Natural log of x: NaN"
Exponential of x (e^x)
# This is a valid calculation.
exp_x <- exp(x)
print(paste("Exponential of x (e^x):", exp_x))
## [1] "Exponential of x (e^x): 0.000335462627902512"
4. The second argument of the rep() function can be modified to
give different patterns.
For example, to get four 2s, then three 3s, then two 5s, enter
rep(c(2,3,5), c(4,3,2))
(a) What is the output from the following command? rep(c(2,3,5), 4:2)
# First, R evaluates the expression 4:2, which creates the vector c(4, 3, 2).
# So, the command rep(c(2,3,5), 4:2) is identical to the
# example rep(c(2,3,5), c(4,3,2)).
# It will repeat '2' four times, '3' three times, and '5' two times.
rep(c(2,3,5), 4:2)
4
## [1] 2 2 2 2 3 3 3 5 5
(b) Obtain a vector of four 4s, four 3s, and four 2s.
# We want to repeat the vector c(4, 3, 2).
# We want 4 repeats for each, so we use the 'times' argument with a
# corresponding vector c(4, 4, 4).
rep(c(4, 3, 2), times = c(4, 4, 4))
## [1] 4 4 4 4 3 3 3 3 2 2 2 2
(c) Use ‘[Link]’ for a vector of length 50
# We provide the sequence c(3, 1, 1, 5, 7) and tell R to keep
# repeating it until the vector's total length is 50.
rep(c(3, 1, 1, 5, 7), [Link] = 50)
## [1] 3 1 1 5 7 3 1 1 5 7 3 1 1 5 7 3 1 1 5 7 3 1 1 5 7 3 1 1 5 7 3 1 1 5 7 3 1 1
## [39] 5 7 3 1 1 5 7 3 1 1 5 7
(d) Use ‘each’ and an alternative method
# The 'each' argument is a simple way to repeat each element
# a specified number of times before moving to the next element.
rep(c(3, 1, 1, 5, 7), each = 4)
## [1] 3 3 3 3 1 1 1 1 1 1 1 1 5 5 5 5 7 7 7 7
# To do this *without* the 'each' argument, we must use 'times'.
# Our input vector c(3, 1, 1, 5, 7) has 5 elements.
# We must provide a 'times' vector that also has 5 elements,
# telling R to repeat each corresponding element 4 times.
rep(c(3, 1, 1, 5, 7), times = c(4, 4, 4, 4, 4))
## [1] 3 3 3 3 1 1 1 1 1 1 1 1 5 5 5 5 7 7 7 7
#5. The ^ symbol denotes exponentiation. Consider the following: #1000*((1+0.075)^5 - 1) # Interest on
$1000, compounded # annually at 7.5% p.a. for five years
(a) Evaluate the above expression
# This calculates the interest on $1000 at 7.5% for 5 years
original_interest <- 1000 * ((1 + 0.075)^5 - 1)
print(original_interest)
## [1] 435.6293
(b) Modify for a 3.5% p.a. rate
5
# We just change the rate, 0.075, to 0.035
new_rate_interest <- 1000 * ((1 + 0.035)^5 - 1)
print(new_rate_interest)
## [1] 187.6863
(c) Change exponent 5 to seq(1, 10)
# This will calculate the interest for ALL years from 1 to 10
vector_interest <- 1000 * ((1 + 0.075)^seq(1, 10) - 1)
print(vector_interest)
## [1] 75.0000 155.6250 242.2969 335.4691 435.6293 543.3015 659.0491
## [8] 783.4778 917.2387 1061.0316