Module # 2 Assignment
> # Create the vector > assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22) > # Original (incorrect) function > myMean <- function(assignment2) { + return(sum(assignment) / length(someData)) + } > # Test the original function (will give an error) > myMean(assignment2) Error in myMean(assignment2) : object 'assignment' not found > # Corrected function > myMean_corrected <- function(assignment2) { + return(sum(assignment2) / length(assignment2)) + } > # Test > myMean_corrected(assignment2) [1] 19.25
The original function fails because it uses variable names that are not defined inside the function. The corrected version succeeds because it consistently uses the function argument assignment2 to calculate the mean.
Comments
Post a Comment