1- Create a function named calculate_area_of_circle that takes one parameter, radius, representing the radius of a circle. The function should calculate and return the area using the formula:
area = đťś‹ Ă— radius^2
Test your function with at least two different radii and print the results.
calculate_area_of_circle <- function (radius) {
area <- pi * radius^2
return(area)
}
radius1 <- 3
radius2 <-10
area1 <- calculate_area_of_circle(radius1)
area2 <- calculate_area_of_circle(radius2)
print(paste("Area of circle with radius", radius1, ":", area1))
## [1] "Area of circle with radius 3 : 28.2743338823081"
print(paste("Area of circle with radius", radius2, ":", area2))
## [1] "Area of circle with radius 10 : 314.159265358979"
2- Write a function called check_temperature that takes a single numeric input temp.
If temp > 30, print “Hot”
If temp >= 15 & temp <= 30, print “Warm”
Otherwise, print “Cold”
Test the function on three different values (e.g., 10, 20, 35).
check_temperature <- function(temp) {
if (temp> 20) {
print("Hot")
} else if (temp >= 12 & temp <=30) {
print("Warm")
} else {
print("Cold")
}
}
check_temperature(10) # Should print "Cold"
## [1] "Cold"
check_temperature(20) # Should print "Warm"
## [1] "Warm"
check_temperature(35) # Should print "Hot"
## [1] "Hot"
3- Write a function named sum_odd_numbers that uses a for loop to calculate the sum of all odd numbers from 1 to n. The function should return the total sum.
Test the function with n = 9 and n = 39.
sum_odd_numbers <- function(n) {
total <- 0
for (i in 1:n) {
if(i %% 2== 1) {
total <- total + i
}
} }
print(paste("Sum of odd numbers up to 9:", sum_odd_numbers(9)))
## [1] "Sum of odd numbers up to 9: "
print(paste("Sum of odd numbers up to 39:", sum_odd_numbers(39)))
## [1] "Sum of odd numbers up to 39: "
For this section, use the built-in mtcars dataset. Perform the following:
Check the structure of the dataset.
Display the summary statistics.
Check for any missing values.
Write a question about the dataset and answer it using functions you learned in this class. Print the output of the answer.
data(mtcars)
classify_hp <- function(hp) {
if (hp > 200) {
print("High")
} else if (hp >= 100 & hp <= 200) {
print("Medium")
} else {
print("Low")
}
}
classify_hp(90)
## [1] "Low"
classify_hp(150)
## [1] "Medium"
classify_hp(220)
## [1] "High"
for (i in 1:nrow(mtcars)) {
car_name <- rownames(mtcars)[i]
hp_class <- classify_hp(mtcars$hp[i])
print(paste("Car:", car_name, "-Horsepower Category:", hp_class))
}
## [1] "Medium"
## [1] "Car: Mazda RX4 -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Mazda RX4 Wag -Horsepower Category: Medium"
## [1] "Low"
## [1] "Car: Datsun 710 -Horsepower Category: Low"
## [1] "Medium"
## [1] "Car: Hornet 4 Drive -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Hornet Sportabout -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Valiant -Horsepower Category: Medium"
## [1] "High"
## [1] "Car: Duster 360 -Horsepower Category: High"
## [1] "Low"
## [1] "Car: Merc 240D -Horsepower Category: Low"
## [1] "Low"
## [1] "Car: Merc 230 -Horsepower Category: Low"
## [1] "Medium"
## [1] "Car: Merc 280 -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Merc 280C -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Merc 450SE -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Merc 450SL -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: Merc 450SLC -Horsepower Category: Medium"
## [1] "High"
## [1] "Car: Cadillac Fleetwood -Horsepower Category: High"
## [1] "High"
## [1] "Car: Lincoln Continental -Horsepower Category: High"
## [1] "High"
## [1] "Car: Chrysler Imperial -Horsepower Category: High"
## [1] "Low"
## [1] "Car: Fiat 128 -Horsepower Category: Low"
## [1] "Low"
## [1] "Car: Honda Civic -Horsepower Category: Low"
## [1] "Low"
## [1] "Car: Toyota Corolla -Horsepower Category: Low"
## [1] "Low"
## [1] "Car: Toyota Corona -Horsepower Category: Low"
## [1] "Medium"
## [1] "Car: Dodge Challenger -Horsepower Category: Medium"
## [1] "Medium"
## [1] "Car: AMC Javelin -Horsepower Category: Medium"
## [1] "High"
## [1] "Car: Camaro Z28 -Horsepower Category: High"
## [1] "Medium"
## [1] "Car: Pontiac Firebird -Horsepower Category: Medium"
## [1] "Low"
## [1] "Car: Fiat X1-9 -Horsepower Category: Low"
## [1] "Low"
## [1] "Car: Porsche 914-2 -Horsepower Category: Low"
## [1] "Medium"
## [1] "Car: Lotus Europa -Horsepower Category: Medium"
## [1] "High"
## [1] "Car: Ford Pantera L -Horsepower Category: High"
## [1] "Medium"
## [1] "Car: Ferrari Dino -Horsepower Category: Medium"
## [1] "High"
## [1] "Car: Maserati Bora -Horsepower Category: High"
## [1] "Medium"
## [1] "Car: Volvo 142E -Horsepower Category: Medium"