Create a function named calculate_area_of_rectangle that takes two parameters, length and width, representing the dimensions of a rectangle. The function should calculate and return the area of the rectangle using the formula area = length * width.
Test your function with different 2 sets of values and print the results.
calculate_area_of_rectangle <-function(length, width) { area <- length * widthreturn(area)}# Test with two sets of valuesresult1 <-calculate_area_of_rectangle(5, 3)result2 <-calculate_area_of_rectangle(10, 4)print(result1)
[1] 15
print(result2)
[1] 40
Design a function called calculate_average that accepts a vector of numeric values as its parameter. The function should calculate the average of the provided values and return the result. Ensure that the function handles the case when an empty vector is passed, and in such cases, it should print a message indicating that the vector is empty. (hint: use if/else)
calculate_average <-function(values) {if (length(values) ==0) {print("The vector is empty.") } else { average <-mean(values)return(average) }}# Test with a vector of numbersprint(calculate_average(c(10, 20, 30, 40)))
[1] 25
# Test with an empty vectorcalculate_average(numeric(0))
[1] "The vector is empty."
Develop a function named check_even_odd that takes an integer as its parameter. The function should determine whether the provided integer is even or odd and print a corresponding message. Test your function on the following values: 14, and 27.
check_even_odd <-function(number) {if (number %%2==0) {print("The number is even.") } else {print("The number is odd.") }}# Test the functioncheck_even_odd(14)
[1] "The number is even."
check_even_odd(27)
[1] "The number is odd."
Dates and Times
Output the date you answered this homework using a function.
Sys.Date()
[1] "2026-10-08"
EDA
Use the airquality dataset and perform the following: 1. Check the structure of the dataset. 2. Check the summary. 3. Check the NA’s.
# 1. Check the structure of the dataset:str(airquality)
'data.frame': 153 obs. of 6 variables:
$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
$ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
$ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
$ Month : int 5 5 5 5 5 5 5 5 5 5 ...
$ Day : int 1 2 3 4 5 6 7 8 9 10 ...
# 2. Check the summary:summary(airquality)
Ozone Solar.R Wind Temp
Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
NAs :37 NAs :7
Month Day
Min. :5.000 Min. : 1.0
1st Qu.:6.000 1st Qu.: 8.0
Median :7.000 Median :16.0
Mean :6.993 Mean :15.8
3rd Qu.:8.000 3rd Qu.:23.0
Max. :9.000 Max. :31.0