-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule_2.R
47 lines (33 loc) · 1.45 KB
/
Module_2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Module 2 - Introduction to basic R functions and Data Structures - assignment
# Student: Sardys Avile-Martinez
assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
myMean <- function(assignment2) { return(sum(assignment2)/length(assignment2)) }
result <- myMean(assignment2)
print(result)
mean(assignment2)
# How can improve this function??
# Improved myMean() function with error handling and NA handling
myMean <- function(data) {
# Check if input is numeric and not empty
if (!is.numeric(data)) {
stop("Error: Input must be a numeric vector.")
}
if (length(data) == 0) {
stop("Error: Input vector is empty.")
}
# This optional - At this point just adding complexity
# Handle missing values by removing them before calculating mean
clean_data <- na.omit(data)
# Calculate the mean
mean_value <- sum(clean_data) / length(clean_data)
return(mean_value)
}
# Well time for testing after adding an extra layer logic to the function
#Test data with missing values
assignment2 <- c(16, 18, 14, NA, 22, 27, 17, 19, 17, 17, 22, 20, 22, NA)
# Call the improved function
result <- myMean(assignment2)
print(result)
# Conclusion: Functions are powerful because of their modularity, reusability, and scalability.
# Whether handling small cases, processing large datasets, or adapting to changing requirements, functions
# offer the flexibility to evolve with our needs or client needs.